经测试代码如下:
/**
* 计算指定目录(包含子目录)的大小
*
* @param string $DirectoryPath Directory path
* @arrange (512.笔记) jb51.cc
**/
function CalcDirectorySize($DirectoryPath) {
$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/');
/*** 代码来自编程之家 jb51.cc(jb51.cc) ***/
原文链接:https://www.f2er.com/php/528958.html