php獲取某個(gè)目錄大小的代碼
更新時(shí)間:2008年09月10日 11:16:28 作者:
大致就是不知道目錄下面又多少層目錄, 也不知道又多少文件, 需要統(tǒng)計(jì)占用空間大小, 這個(gè)可以用在 相冊/數(shù)據(jù)庫占用/網(wǎng)絡(luò)U盤 等程序中.
大致程序思想就是使用遞規(guī)來計(jì)算目錄占用空間多少, 然后再把這個(gè)占用空間的值寫進(jìn)文本文件里, 那么只要訪問這個(gè)txt文件就知道占用了多少空間, 不用頻繁獲取而讀磁盤, 節(jié)省資源. 每次用戶如果上傳的文件或者刪除了文件, 那么又重新進(jìn)行統(tǒng)計(jì). 當(dāng)然, 也可以把統(tǒng)計(jì)結(jié)果保存到數(shù)據(jù)庫里.
function countDirSize($dir)
{
$handle = opendir($dir);
while (false!==($FolderOrFile = readdir($handle)))
{
if($FolderOrFile != "." && $FolderOrFile != "..")
{
if(is_dir("$dir/$FolderOrFile")) {
$sizeResult += getDirSize("$dir/$FolderOrFile");
} else {
$sizeResult += filesize("$dir/$FolderOrFile");
}
}
}
closedir($handle);
return $sizeResult;
}
把字節(jié)轉(zhuǎn)換為正常的K啊M啊之類的函數(shù):
function get_real_size($size) {
$kb = 1024; // Kilobyte
$mb = 1024 * $kb; // Megabyte
$gb = 1024 * $mb; // Gigabyte
$tb = 1024 * $gb; // Terabyte
if($size < $kb) {
return $size." B";
}else if($size < $mb) {
return round($size/$kb,2)." KB";
}else if($size < $gb) {
return round($size/$mb,2)." MB";
}else if($size < $tb) {
return round($size/$gb,2)." GB";
}else {
return round($size/$tb,2)." TB";
}
}
用法很簡單:
$size_zip=countDirSize("../zip/");
$size_zip=get_real_size($size_zip);
就這么簡單了,呵呵。
<?
/**
* File: fetch user directory use size
* Author: heiyeluren <heiyeluren_AT_gmail_com>
* Create: 2005-9-19 16:20
* Modifed: 2005-9-19 16:41
*/
/*** 基本函數(shù) ***/
//計(jì)算目錄大小
function countDirSize(dir)
{
handle = opendir(dir);
while (false!==(FolderOrFile = readdir(handle)))
{
if(FolderOrFile != "." && FolderOrFile != "..")
{
if(is_dir("dir/FolderOrFile")) {
sizeResult += getDirSize("dir/FolderOrFile");
} else {
sizeResult += filesize("dir/FolderOrFile");
}
}
}
closedir(handle);
return sizeResult;
}
//保存用戶文件大小
function saveDirSize(userDir)
{
userDirSize = countDirSize(userDir);
if (!fp = fopen(userDir."/dir_size.txt", "w+")) {
die("Open file failed");
} else {
fwrite(fp, dirSize);
}
}
//獲取用戶目錄的大小
function getDirSize(userDir)
{
user = addslashes(userDir);
sizeFile = userDir."/dir_size.txt";
if (!fp = fopen(sizeFile, "r") {
return 0;
} else {
dirSize = fread(fp, filesize(sizeFile));
}
return dirSize;
}
/*** 調(diào)用實(shí)例 ***/
user = "heiyeluren";
userPath = "./user/".user;
//如果用戶執(zhí)行了刪除或者上傳文件的操作就重新獲取目錄大小
if (action == "upload" || action == "delete") {
saveDirSize(userPath);
}
userDirSize = getDirSize(userPath)/1024;
echo "用戶: ".user;
echo "占用空間: ".userDirSize;
?>
復(fù)制代碼 代碼如下:
function countDirSize($dir)
{
$handle = opendir($dir);
while (false!==($FolderOrFile = readdir($handle)))
{
if($FolderOrFile != "." && $FolderOrFile != "..")
{
if(is_dir("$dir/$FolderOrFile")) {
$sizeResult += getDirSize("$dir/$FolderOrFile");
} else {
$sizeResult += filesize("$dir/$FolderOrFile");
}
}
}
closedir($handle);
return $sizeResult;
}
把字節(jié)轉(zhuǎn)換為正常的K啊M啊之類的函數(shù):
復(fù)制代碼 代碼如下:
function get_real_size($size) {
$kb = 1024; // Kilobyte
$mb = 1024 * $kb; // Megabyte
$gb = 1024 * $mb; // Gigabyte
$tb = 1024 * $gb; // Terabyte
if($size < $kb) {
return $size." B";
}else if($size < $mb) {
return round($size/$kb,2)." KB";
}else if($size < $gb) {
return round($size/$mb,2)." MB";
}else if($size < $tb) {
return round($size/$gb,2)." GB";
}else {
return round($size/$tb,2)." TB";
}
}
用法很簡單:
復(fù)制代碼 代碼如下:
$size_zip=countDirSize("../zip/");
$size_zip=get_real_size($size_zip);
就這么簡單了,呵呵。
復(fù)制代碼 代碼如下:
<?
/**
* File: fetch user directory use size
* Author: heiyeluren <heiyeluren_AT_gmail_com>
* Create: 2005-9-19 16:20
* Modifed: 2005-9-19 16:41
*/
/*** 基本函數(shù) ***/
//計(jì)算目錄大小
function countDirSize(dir)
{
handle = opendir(dir);
while (false!==(FolderOrFile = readdir(handle)))
{
if(FolderOrFile != "." && FolderOrFile != "..")
{
if(is_dir("dir/FolderOrFile")) {
sizeResult += getDirSize("dir/FolderOrFile");
} else {
sizeResult += filesize("dir/FolderOrFile");
}
}
}
closedir(handle);
return sizeResult;
}
//保存用戶文件大小
function saveDirSize(userDir)
{
userDirSize = countDirSize(userDir);
if (!fp = fopen(userDir."/dir_size.txt", "w+")) {
die("Open file failed");
} else {
fwrite(fp, dirSize);
}
}
//獲取用戶目錄的大小
function getDirSize(userDir)
{
user = addslashes(userDir);
sizeFile = userDir."/dir_size.txt";
if (!fp = fopen(sizeFile, "r") {
return 0;
} else {
dirSize = fread(fp, filesize(sizeFile));
}
return dirSize;
}
/*** 調(diào)用實(shí)例 ***/
user = "heiyeluren";
userPath = "./user/".user;
//如果用戶執(zhí)行了刪除或者上傳文件的操作就重新獲取目錄大小
if (action == "upload" || action == "delete") {
saveDirSize(userPath);
}
userDirSize = getDirSize(userPath)/1024;
echo "用戶: ".user;
echo "占用空間: ".userDirSize;
?>
相關(guān)文章
php將圖片文件轉(zhuǎn)換成二進(jìn)制輸出的方法
這篇文章主要介紹了php將圖片文件轉(zhuǎn)換成二進(jìn)制輸出的方法,涉及php針對(duì)圖片文件的讀取與轉(zhuǎn)換輸出技巧,需要的朋友可以參考下2015-06-06編寫php應(yīng)用程序?qū)崿F(xiàn)摘要式身份驗(yàn)證的方法詳解
本篇文章是對(duì)編寫php應(yīng)用程序?qū)崿F(xiàn)摘要式身份驗(yàn)證的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06php簡單定時(shí)執(zhí)行任務(wù)的實(shí)現(xiàn)方法
這篇文章主要介紹了php簡單定時(shí)執(zhí)行任務(wù)的實(shí)現(xiàn)方法,涉及curl及sleep等操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-02-02詳解PHP實(shí)現(xiàn)HTTP服務(wù)器過程
一般來講,PHP很少談到“實(shí)現(xiàn)HTTP服務(wù)”的說法,因?yàn)閺脑缙诘腃GI到后來的PHP-FPM,官方已經(jīng)給出了最穩(wěn)定的HTTP解決方案,你只要配合一個(gè)Apache或Nginx類的服務(wù)器就能實(shí)現(xiàn)穩(wěn)定的HTTP服務(wù)2023-02-02PHP棧的定義、入棧出棧方法及基于堆棧實(shí)現(xiàn)的計(jì)算器完整實(shí)例
這篇文章主要介紹了PHP棧的定義、入棧出棧方法及基于堆棧實(shí)現(xiàn)的計(jì)算器,結(jié)合實(shí)例形式較為詳細(xì)的分析了php定義與使用棧的基本方法,并結(jié)合完整實(shí)例形式給出了php基于堆棧實(shí)現(xiàn)高級(jí)計(jì)算器功能的相關(guān)操作技巧,需要的朋友可以參考下2017-11-11