0

for a filemanagement i calculate the size of each file and the directories in a specific folder. I calculate the size of the files like below:

 sizeFormat(filesize($dir . '/' . $file))

and the size of the directories (included subdirs) like below:

sizeFormat(recursive_directory_size($dir . '/' . $file))

How can i calculate now the sum size of all files and directories in that specific folder?

Jack Maessen
  • 1,780
  • 4
  • 19
  • 51

1 Answers1

0

Try this function, it is what you need, I hope.

function dirsize($dir) {
 if(is_file($dir)) return array('size'=>filesize($dir));
  if($dh=opendir($dir)) {
    $size=0;
    $n = 0;
    while(($file=readdir($dh))!==false) {
        if($file=='.' || $file=='..') continue;
        $n++;
        $data = dirsize($dir.'/'.$file);
        $size += $data['size'];
    }
    closedir($dh);
    return array('size'=>$size);
  } 
   return array('size'=>0);
 }

print_r(dirsize("bitnami.css"));
print_r(dirsize("some_folder"));