filesize()
函数在 PHP 中用于获取文件的大小,而不是目录的大小
要获取目录的大小,你可以使用以下代码:
function getDirectorySize($directory) {
$totalSize = 0;
$objects = scandir($directory);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (is_dir($directory . DIRECTORY_SEPARATOR . $object) && !is_link($directory . DIRECTORY_SEPARATOR . $object)) {
$totalSize += getDirectorySize($directory . DIRECTORY_SEPARATOR . $object);
} else {
$totalSize += filesize($directory . DIRECTORY_SEPARATOR . $object);
}
}
}
return $totalSize;
}
$directory = "path/to/your/directory";
echo "Size of directory: " . getDirectorySize($directory) . " bytes";
这个函数会递归地遍历目录及其子目录,并将所有文件和子目录的大小累加起来。请将 "path/to/your/directory"
替换为你要获取大小的目录路径。