使用filesize()获取目录的总文件大小及其内容[重复]

This question already has an answer here:

I'm using filesize() to hopefully return the total file size of an image directory. The code returns a size of 24mb which I know is incorrect, the directory has around 200+mb of images. Any ideas?

<?
    $filename = '../uploads';
    echo (filesize($filename) / 1024) . ' mb';
?>
</div>

to get directory size : source

function dir_size($directory) {
    $size = 0;
    foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $file){
        $size += $file->getSize();
    }
    return $size;
}

format the return value with following function to get human readable value : 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];
}