Posted By

stancell on 09/25/08


Tagged

folder size directory


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

carlosabargues
vali29


Calculate directory size


 / Published in: PHP
 

  1. /**
  2.  * Calculate directory size.
  3.  * @param string $path without trailing '/' or '\' (eg. '/users/sampleUser', not '/users/sampleUser/')
  4.  * @return int size in bytes
  5.  */
  6. function calc_dir_size($path)
  7. {
  8. $size = 0;
  9.  
  10. if ($handle = opendir($path))
  11. {
  12. while (false !== ($entry = readdir($handle)))
  13. {
  14. $current_path = $path . '/' . $entry;
  15. if ($entry != '.' && $entry != '..' && !is_link($current_path))
  16. {
  17. if (is_file($current_path))
  18. $size += filesize($current_path);
  19. elseif (is_dir($current_path))
  20. $size = calc_dir_size($current_path);
  21. }
  22. }
  23. }
  24. closedir($handle);
  25. return $size;
  26. }

Report this snippet  

You need to login to post a comment.