A PHP function for statistical directory file size

Source: Internet
Author: User

Just came to the company in the morning, the head told me, grasp to write a small function, used to count the file size in the specified directory, I went, do it, fortunately a little foundation, a will be completed, haha. The code is below.
  1. <?
  2. /**
  3. Functions for Statistics directory file size
  4. @author Xfcode
  5. @link http://www.jbxue.com
  6. */
  7. function Dirsize ($dir)
  8. {
  9. @ $dh = Opendir ($dir);
  10. $size = 0;
  11. While ($file = @readdir ($DH))
  12. {
  13. if ($file ! = "." and $file! = "...")
  14. {
  15. $path = $dir."/". $file;
  16. if (Is_dir ($path))
  17. {
  18. $size + = Dirsize ($path);
  19. }
  20. ElseIf (Is_file ($path))
  21. {
  22. $size + = FileSize ($path);
  23. }
  24. }
  25. }
  26. @closedir ($DH);
  27. return $size;
  28. }
  29. Function end
  30. eg
  31. $dir _path = "./my_files";
  32. $dir _size = dirsize ($dir _path);
  33. $dir _size = $dir _size/1024/1024;
  34. Echo $dir _size.  "MB";
  35. ?>

This function recursively iterates through all the files in the directory and calculates the total file size in megabytes.
Rookie moves, the big boys are laughed at.

A PHP function that has a statistical directory file size

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.