Dir size recursive php function


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

Calculate dir size recursive, follow the link above because the version above is better.


Copy this code and paste it in your HTML
  1. function dirsize($dir,$buf=2) {
  2. static $buffer;
  3. if(isset($buffer[$dir])) return $buffer[$dir];
  4. if(is_file($dir)) return filesize($dir);
  5. if($dh=opendir($dir)) {
  6. $size=0;
  7. while(($file=readdir($dh))!==false) {
  8. if($file=='.' || $file=='..') continue;
  9. $size+=dirsize($dir.'/'.$file,$buf-1);
  10. }
  11. closedir($dh);
  12. if($buf>0) $buffer[$dir]=$size;
  13. return $size;
  14. }
  15. return false;
  16. }

URL: http://www.barattalo.it/2010/02/01/calculate-dir-size-recursively/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.