PHP Gets the folder size function

Source: Internet
Author: User
  1. Get 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. Unit Automatic conversion function
  23. function Getrealsize ($size)
  24. {
  25. $KB = 1024; Kilobyte
  26. $MB = 1024x768 * $KB; Megabyte
  27. $GB = 1024x768 * $MB; Gigabyte
  28. $TB = 1024x768 * $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
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.