PHP 统计目录下文件数和文件大小

 1 /**
 2  * 统计文件数和文件大小
 3  */
 4 private function getFileCacheCount($pathName)
 5 {
 6     $data = [
 7         'num' => 0,
 8         'size' => 0
 9     ];
10     if (file_exists($pathName)) {
11         $dir = opendir($pathName);
12         while(false !== ($file_name = readdir($dir))){
13             if(!($file_name == "." || $file_name == "..")){
14                 $fileName = $pathName . "/" . $file_name;
15                 if(is_dir($fileName)){
16                     $subData = $this->getFileCacheCount($fileName);
17                     $data['size'] += $subData['size'];
18                     $data['num'] += $subData['num'];
19                 }else{
20                     $data['size'] += filesize($fileName);
21                     $data['num']++;
22                 }
23             }
24         }
25         closedir($dir);
26     }
27     return $data;
28 }

 

posted @ 2018-01-27 19:44  py卡卡  阅读(475)  评论(0编辑  收藏  举报