Home  >  Article  >  Backend Development  >  php function to get folder size

php function to get folder size

WBOY
WBOYOriginal
2016-07-25 08:56:041003browse
  1. // Get the folder size
  2. function getDirSize($dir)
  3. {
  4. $handle = opendir($dir);
  5. while (false!==($FolderOrFile = readdir($handle )))
  6. {
  7. if($FolderOrFile != "." && $FolderOrFile != "..")
  8. {
  9. if(is_dir("$dir/$FolderOrFile"))
  10. {
  11. $sizeResult += getDirSize( "$dir/$FolderOrFile");
  12. }
  13. else
  14. { bbs.it-home.org
  15. $sizeResult += filesize("$dir/$FolderOrFile");
  16. }
  17. }
  18. }
  19. closedir($handle) ;
  20. return $sizeResult;
  21. }
  22. // Automatic unit conversion function
  23. function getRealSize($size)
  24. {
  25. $kb = 1024; // Kilobyte
  26. $mb = 1024 * $kb; // Megabyte
  27. $gb = 1024 * $mb; // Gigabyte
  28. $tb = 1024 * $gb; // Terabyte
  29. if($size < $kb)
  30. {
  31. return $size." B";
  32. }
  33. else if($size < ; $mb)
  34. {
  35. return round($size/$kb,2)." KB";
  36. }
  37. else if($size < $gb)
  38. {
  39. return round($size/$mb,2) ." MB";
  40. }
  41. else if($size < $tb)
  42. {
  43. return round($size/$gb,2)." GB";
  44. }
  45. else
  46. {
  47. return round($size/ $tb,2)." TB";
  48. }
  49. }
  50. echo getRealSize(getDirSize('Directory'));
  51. ?>
Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn