Share Pastebin
Guest
Public paste!

Sam Yong

By: a guest | Aug 30th, 2008 | Syntax: PHP | Size: 0.92 KB | Hits: 136 | Expires: Never
Copy text to clipboard
  1. /************************************
  2.  
  3.  Recursive directory size calculation function
  4.  http://thephpcode.blogspot.com/
  5.  
  6.  Author: Sam Yong
  7.  blog: http://thephpdeveloper.blogspot.com/
  8.  
  9.  No illegal use of this script is allowed.
  10.  
  11. ************************************/
  12.  
  13. function dirsize($dirname) {
  14. if (!is_dir($dirname) || !is_readable($dirname)) {
  15. // check whether the directory is valid.
  16. return false;
  17. }
  18.  
  19. $dirname_stack[] = $dirname;
  20. $size = 0;
  21.  
  22. do {
  23. $dirname = array_shift($dirname_stack);
  24. $handle = opendir($dirname);
  25. while (false !== ($file = readdir($handle))) {
  26. if ($file != '.' && $file != '..' && is_readable($dirname . DIRECTORY_SEPARATOR . $file)) {
  27. if (is_dir($dirname . DIRECTORY_SEPARATOR . $file)) {
  28. $dirname_stack[] = $dirname . DIRECTORY_SEPARATOR . $file;
  29. }
  30. $size += filesize($dirname . DIRECTORY_SEPARATOR . $file);
  31. }
  32. }
  33. closedir($handle);
  34. } while (count($dirname_stack) > 0);
  35.  
  36. return $size;
  37. }