php計算整個目錄大小的方法
更新時間:2015年06月01日 15:21:07 作者:不吃皮蛋
這篇文章主要介紹了php計算整個目錄大小的方法,涉及php遞歸遍歷與文件操作的相關技巧,需要的朋友可以參考下
本文實例講述了php計算整個目錄大小的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
/**
* Calculate the full size of a directory
*
* @author Jonas John
* @version 0.2
* @link http://www.jonasjohn.de/snippets/php/dir-size.htm
* @param string $DirectoryPath Directory path
*/
function CalcDirectorySize($DirectoryPath) {
// I reccomend using a normalize_path function here
// to make sure $DirectoryPath contains an ending slash
// (-> http://www.jonasjohn.de/snippets/php/normalize-path.htm)
// To display a good looking size you can use a readable_filesize
// function.
// (-> http://www.jonasjohn.de/snippets/php/readable-filesize.htm)
$Size = 0;
$Dir = opendir($DirectoryPath);
if (!$Dir)
return -1;
while (($File = readdir($Dir)) !== false) {
// Skip file pointers
if ($File[0] == '.') continue;
// Go recursive down, or add the file size
if (is_dir($DirectoryPath . $File))
$Size += CalcDirectorySize($DirectoryPath . $File . DIRECTORY_SEPARATOR);
else
$Size += filesize($DirectoryPath . $File);
}
closedir($Dir);
return $Size;
}
//使用范例:
$SizeInBytes = CalcDirectorySize('data/');
希望本文所述對大家的php程序設計有所幫助。
相關文章
PHP基于curl后臺遠程登錄正方教務系統(tǒng)的方法
這篇文章主要介紹了PHP基于curl后臺遠程登錄正方教務系統(tǒng)的方法,結(jié)合實例形式分析了php使用curl及cookie實現(xiàn)遠程登陸的操作技巧,需要的朋友可以參考下2016-10-10
php基于ob_start(ob_gzhandler)實現(xiàn)網(wǎng)頁壓縮功能的方法
這篇文章主要介紹了php基于ob_start('ob_gzhandler')實現(xiàn)網(wǎng)頁壓縮功能的方法,涉及php中ob_gzip、ob_start等函數(shù)操作緩沖區(qū)及內(nèi)容壓縮相關技巧,需要的朋友可以參考下2017-02-02
在mysql數(shù)據(jù)庫原有字段后增加新內(nèi)容
在mysql數(shù)據(jù)庫原有字段后增加新內(nèi)容2009-11-11
php遞歸創(chuàng)建和刪除文件夾的代碼小結(jié)
有時候需要遞歸創(chuàng)建和刪除文件夾,那么就可以參考下面的代碼2012-04-04
php基于雙向循環(huán)隊列實現(xiàn)歷史記錄的前進后退等功能
這篇文章主要介紹了php基于雙向循環(huán)隊列實現(xiàn)歷史記錄的前進后退等功能,較為詳細的分析了php使用歷史記錄功能所涉及的相關技巧與實現(xiàn)方法,具有一定參考借鑒價值,需要的朋友可以參考下2015-08-08

