-2

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';
?>
JoshMc
  • 669
  • 5
  • 15
  • There are several solutions already on this site. You should take a look at [some existing answers](https://www.google.com/search?q=php+folder+size+site%3Astackoverflow.com) before posting a new question here. Perhaps someone has already provided a solution that will work for you. – Lix Sep 08 '13 at 12:20

1 Answers1

4

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];
}
Community
  • 1
  • 1
Janith Chinthana
  • 3,792
  • 2
  • 27
  • 54