Php statistics function for getting folder information

Source: Internet
Author: User
Php statistics function for getting folder information

  1. // Collect information about folders

  2. // Count the number of directories
  3. // Format the size of the output directory. unit: Bytes, KB, MB, and GB.
  4. Function getFolderSize ($ 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 = getFolderSize ($ 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. // Format the output information

  39. Function sizeFormat ($ size)
  40. {
  41. $ SizeStr = '';
  42. If ($ size <1024)
  43. {
  44. Return $ size. "bytes ";
  45. }
  46. Else if ($ size <(1024*1024 ))
  47. {
  48. $ Size = round ($ size/1024,1 );
  49. Return $ size. "KB ";
  50. }
  51. Else if ($ size <(1024*1024*1024 ))
  52. {
  53. $ Size = round ($ size/(1024*1024), 1 );
  54. Return $ size. "MB ";
  55. } Bbs.it-home.org
  56. Else
  57. {
  58. $ Size = round ($ size/(1024x1024*1024), 1 );
  59. Return $ size. "GB ";
  60. }
  61. }
  62. $ Path = "/var/www ";
  63. $ Ar = getFolderSize ($ path );
  64. Echo "the path you are viewing: $ path ";
  65. Echo "directory size:". sizeFormat ($ ar ['size'])."
    ";
  66. Echo "file count:". $ ar ['count']."
    ";
  67. Echo "directory count:". $ ar ['dircount']."
    ";

  68. // Output

  69. // Print_r ($ ar );
  70. ?>

Example 2: 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. {
  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 path '));
  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.