我正在使用filesize()来希望返回图像目录的总文件大小。代码返回24mb的大小,我知道这是不正确的,该目录有大约200 + mb的图像。有什么想法吗?
<?
$filename = '../uploads';
echo (filesize($filename) / 1024) . ' mb';
?>
答案 0 :(得分:3)
获取目录大小:source
function dir_size($directory) {
$size = 0;
foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $file){
$size += $file->getSize();
}
return $size;
}
使用以下函数格式化返回值以获得人类可读的值:source
function format_size($size) {
$mod = 1024;
$units = explode(' ','B KB MB GB TB PB');
for ($i = 0; $size > $mod; $i++) {
$size /= $mod;
}
return round($size, 2) . ' ' . $units[$i];
}