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"
替換為你要獲取大小的目錄路徑。