如何用PHP递归计算目录下所有的文件的大小总和

学习笔记 马富天 2016-04-18 13:17:56 83 0

【摘要】使用PHP计算指定目录下所有文件的大小的总和,使用的是递归的方法。涉及的PHP函数有:opendir()、close_dir()、is_dir()、is_file()、readdir()、filesize()、file_exists()等。

很多时候我们都需要计算自己网页空间的使用量,需要知道已经占用的空间大小,因此我写了一个递归函数计算指定目录下所有文件的大小总和。

  1.     function getFileTotSize($dir,&$count){
  2.         if(!isset($count['size'])){
  3.             $count['size'] = 0;
  4.         }
  5.         if(!isset($count['dir'])){
  6.             $count['dir'] = 0;           
  7.         }
  8.         if(!isset($count['file'])){
  9.             $count['file'] = 0;           
  10.         }
  11.         if(!isset($count['html'])){
  12.             $count['html'] = 0;           
  13.         }   
  14.         if(!isset($count['jpg'])){
  15.             $count['jpg'] = 0;           
  16.         }
  17.         if(!isset($count['png'])){
  18.             $count['png'] = 0;           
  19.         }
  20.         if(!isset($count['php'])){
  21.             $count['php'] = 0;           
  22.         }                                              
  23.         if(!file_exists($dir)){
  24.             echo "目录不存在";
  25.         }else{
  26.             if(is_dir($dir)){
  27.                 if($handle=opendir($dir)){
  28.                     while($list=readdir($handle)){
  29.                         if($list != "." && $list != ".."){
  30.                             $path = $dir."/".$list;
  31.                             if(is_file($path)){
  32.                                 $count['file'] += 1;
  33.                                 $count['size'] += filesize($path);
  34.                                 switch(getFileType($path)){
  35.                                     case "html":
  36.                                             $count['html'] += 1;
  37.                                         break;
  38.                                     case "jpg":
  39.                                             $count['jpg'] += 1;
  40.                                         break;
  41.                                     case "png":
  42.                                             $count['png'] += 1;
  43.                                         break;                                        
  44.                                     case "php":
  45.                                             $count['php'] += 1;
  46.                                         break;                                        
  47.                                 }
  48.                             }
  49.                             if(is_dir($path)){
  50.                                 $count['dir'] += 1;
  51.                                 getFileTotSize($path,$count);
  52.                             }
  53.                             
  54.                         } 
  55.                     }
  56.                 }   
  57.             }
  58.             closedir($handle);
  59.         }
  60.         return $count;
  61.     }
  62.     /**
  63.      * 获取文件类型2
  64.      * @param string $filename 文件名称
  65.      * @return string 文件类型
  66.      */
  67.     function getFileType($filename) {
  68.         $info = pathinfo($filename);
  69.         return strtolower($info['extension']);
  70.     }

版权归 马富天个人博客 所有

本文标题:《如何用PHP递归计算目录下所有的文件的大小总和》

本文链接地址:http://www.mafutian.com/39.html

转载请务必注明出处,小生将不胜感激,谢谢! 喜欢本文或觉得本文对您有帮助,请分享给您的朋友 ^_^

0

0

上一篇《 PHP怎么实现自动登录? 》 下一篇《 使用SQL语句计算出MySQL数据库已使用的空间大小 》

暂无评论

评论审核未开启
表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情
验证码

TOP10

  • 浏览最多
  • 评论最多