Home  >  Article  >  Backend Development  >  统计索引文件大小的php函数

统计索引文件大小的php函数

WBOY
WBOYOriginal
2016-06-13 12:52:09743browse

统计目录文件大小的php函数
早上刚到公司,头告诉我,抓紧写一个小函数,用来统计指定目录中文件大小,我了个去,动手吧,还好有点小基础,一会就完工了,哈哈。代码在下面咯。

 
/**
  统计目录文件大小的函数
  @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";
 ?>

这个函数可以递归遍历目录中的所有文件,并计算以MB为单位的文件总大小。
新手出招,大佬们见笑了。
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