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...
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn