A php function that counts the size of files in a directory.
I just arrived at the company this morning and my boss told me to hurry up and write a small function to count the size of files in a specified directory. I got it. Go ahead and do it, luckily you have a little basic knowledge and you’ll be done in a while, haha. The code is below.
-
- 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;
- }
-
- $dir_path = "./my_files";
- $dir_size = dirsize($dir_path);
- $dir_size = $dir_size/1024/1024;
- echo $dir_size."MB";
- ?>
This function can recursively loop through all files in a directory and calculate the total file size in MB.
The newbie makes a move, and the bosses laugh.
http://www.bkjia.com/PHPjc/1084385.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1084385.htmlTechArticleA php function that counts the size of directory files. I just arrived at the company this morning and my boss told me to hurry up and write a small function. Used to count the size of files in a specified directory. Let me go and do it. Fortunately...