Php function for obtaining the folder size

Source: Internet
Author: User
Php function for obtaining the folder size

  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. ?>

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.