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.
- <?
- /**
- Functions for Statistics directory file size
- @author Xfcode
- @link http://www.jbxue.com
- */
- function Dirsize ($dir)
- {
- @ $dh = Opendir ($dir);
- $size = 0;
- While ($file = @readdir ($DH))
- {
- if ($file ! = "." and $file! = "...")
- {
- $path = $dir."/". $file;
- if (Is_dir ($path))
- {
- $size + = Dirsize ($path);
- }
- ElseIf (Is_file ($path))
- {
- $size + = FileSize ($path);
- }
- }
- }
- @closedir ($DH);
- return $size;
- }
- Function end
- eg
- $dir _path = "./my_files";
- $dir _size = dirsize ($dir _path);
- $dir _size = $dir _size/1024/1024;
- Echo $dir _size. "MB";
- ?>
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