0x00000050電腦藍屏代碼 php擷取某個目錄大小的代碼

來源:互聯網
上載者:User
大致程式思想就是使用遞規來計算目錄佔用空間多少, 然後再把這個佔用空間的值寫進文字檔裡, 那麼只要訪問這個txt檔案就知道佔用了多少空間, 不用頻繁擷取而讀磁碟, 節省資源. 每次使用者如果上傳的檔案或者刪除了檔案, 那麼又重新進行統計. 當然, 也可以把統計結果儲存到資料庫裡.

複製代碼 代碼如下:


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;
}


把位元組轉換為正常的K啊M啊之類的函數:

複製代碼 代碼如下:


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
* Create: 2005-9-19 16:20
* Modifed: 2005-9-19 16:41
*/
/*** 基本函數 ***/
//計算目錄大小
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;
}
/*** 調用執行個體 ***/
user = "heiyeluren";
userPath = "./user/".user;
//如果使用者執行了刪除或者上傳檔案的操作就重新擷取目錄大小
if (action == "upload" || action == "delete") {
saveDirSize(userPath);
}
userDirSize = getDirSize(userPath)/1024;
echo "使用者: ".user;
echo "佔用空間: ".userDirSize;
?>

以上就介紹了0x00000050電腦藍屏代碼 php擷取某個目錄大小的代碼,包括了0x00000050電腦藍屏代碼方面的內容,希望對PHP教程有興趣的朋友有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.