Calculate directory size


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.