php獲取某個目錄大小的代碼
更新時間:2008年09月10日 11:16:28 作者:
大致就是不知道目錄下面又多少層目錄, 也不知道又多少文件, 需要統(tǒng)計占用空間大小, 這個可以用在 相冊/數(shù)據(jù)庫占用/網(wǎng)絡(luò)U盤 等程序中.
大致程序思想就是使用遞規(guī)來計算目錄占用空間多少, 然后再把這個占用空間的值寫進文本文件里, 那么只要訪問這個txt文件就知道占用了多少空間, 不用頻繁獲取而讀磁盤, 節(jié)省資源. 每次用戶如果上傳的文件或者刪除了文件, 那么又重新進行統(tǒng)計. 當(dāng)然, 也可以把統(tǒng)計結(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ù) ***/
//計算目錄大小
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)用實例 ***/
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ù) ***/
//計算目錄大小
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)用實例 ***/
user = "heiyeluren";
userPath = "./user/".user;
//如果用戶執(zhí)行了刪除或者上傳文件的操作就重新獲取目錄大小
if (action == "upload" || action == "delete") {
saveDirSize(userPath);
}
userDirSize = getDirSize(userPath)/1024;
echo "用戶: ".user;
echo "占用空間: ".userDirSize;
?>
相關(guān)文章
編寫php應(yīng)用程序?qū)崿F(xiàn)摘要式身份驗證的方法詳解
本篇文章是對編寫php應(yīng)用程序?qū)崿F(xiàn)摘要式身份驗證的方法進行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06php簡單定時執(zhí)行任務(wù)的實現(xiàn)方法
這篇文章主要介紹了php簡單定時執(zhí)行任務(wù)的實現(xiàn)方法,涉及curl及sleep等操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-02-02PHP棧的定義、入棧出棧方法及基于堆棧實現(xiàn)的計算器完整實例
這篇文章主要介紹了PHP棧的定義、入棧出棧方法及基于堆棧實現(xiàn)的計算器,結(jié)合實例形式較為詳細(xì)的分析了php定義與使用棧的基本方法,并結(jié)合完整實例形式給出了php基于堆棧實現(xiàn)高級計算器功能的相關(guān)操作技巧,需要的朋友可以參考下2017-11-11