A statistical directory file size PHP function, _php tutorial

Source: Internet
Author: User

A PHP function that is a statistical directory file size,


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. Functions for Statistics directory file size
  3. @author Xfcode
  4. @link http://www.jbxue.com
  5. */
  6. function Dirsize ($dir)
  7. {
  8.    @ $dh = Opendir ($dir);
  9. $size = 0;
  10.   while ($file = @readdir ($DH))
  11. {
  12.    if ($file ! = "." and $file! = "...")
  13. {
  14. $path = $dir."/". $file;
  15.      if (Is_dir ($path))
  16. {
  17. $size + = Dirsize ($path);
  18. }
  19. ElseIf (Is_file ($path))
  20. {
  21. $size + = FileSize ($path);
  22. }
  23. }
  24. }
  25.  @closedir ($DH);
  26.  return $size;
  27. }
  28. Function end
  29. eg
  30. $dir _path = "./my_files";
  31. $dir _size = dirsize ($dir _path);
  32. $dir _size = $dir _size/1024/1024x768;
  33. Echo $dir _size. "MB";
  34. ?>

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.

http://www.bkjia.com/PHPjc/1084385.html www.bkjia.com true http://www.bkjia.com/PHPjc/1084385.html techarticle A statistical directory file size PHP function, just came to the company in the morning, the head told me, hurry to write a small function, used to count the file size in the specified directory, I have a go, do it, okay ...

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