PHP calculates the specified folder information (number of folders, number of files, folder size)

Source: Internet
Author: User
  1. Format output Directory size unit: BYTES,KB,MB,GB
  2. can also be used to count the number of catalogs
  3. Site bbs.it-home.org
  4. function Getdirectorysize ($path)
  5. {
  6. $totalsize = 0;
  7. $totalcount = 0;
  8. $dircount = 0;
  9. if ($handle = Opendir ($path))
  10. {
  11. while (false!== ($file = Readdir ($handle)))
  12. {
  13. $nextpath = $path. '/' . $file;
  14. if ($file! = '. ' && $file! = ': ' &&!is_link ($nextpath))
  15. {
  16. if (Is_dir ($nextpath))
  17. {
  18. $dircount + +;
  19. $result = Getdirectorysize ($nextpath);
  20. $totalsize + = $result [' Size '];
  21. $totalcount + = $result [' count '];
  22. $dircount + = $result [' Dircount '];
  23. }
  24. ElseIf (Is_file ($nextpath))
  25. {
  26. $totalsize + = FileSize ($nextpath);
  27. $totalcount + +;
  28. }
  29. }
  30. }
  31. }
  32. Closedir ($handle);
  33. $total [' size '] = $totalsize;
  34. $total [' count '] = $totalcount;
  35. $total [' dircount '] = $dircount;
  36. return $total;
  37. }
  38. function Sizeformat ($size)
  39. {
  40. $SIZESTR = ";
  41. if ($size <1024)
  42. {
  43. return $size. "bytes";
  44. }
  45. else if ($size < (1024*1024))
  46. {
  47. $size =round ($size/1024,1);
  48. return $size. " KB ";
  49. }
  50. else if ($size < (1024*1024*1024))
  51. {
  52. $size =round ($size/(1024*1024), 1);
  53. return $size. " MB ";
  54. }
  55. Else
  56. {
  57. $size =round ($size/(1024*1024*1024), 1);
  58. return $size. " GB ";
  59. }
  60. }
  61. $path = "/home/www/htdocs";
  62. $ar =getdirectorysize ($path);
  63. echo "

    Path: $path

    ";
  64. echo "Directory Size:". Sizeformat ($ar [' size ']). "
    ";
  65. echo "Number of files:". $ar [' Count ']. "
    ";
  66. echo "Catalogue:". $ar [' Dircount ']. "
    ";
  67. Print_r ($ar);
  68. ?>
Copy Code
  • 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.