PHP 统计文件数和文件大小

/**
* 统计文件数和文件大小
*/
private function getFileCacheCount($pathName)
{
$data = [
'num' => 0,
'size' => 0
];
$dir = opendir($pathName);
while(false !== ($file_name = readdir($dir))){
if(!($file_name == "." || $file_name == "..")){
$fileName = $pathName . "/" . $file_name;
if(is_dir($fileName)){
$subData = $this->getFileCacheCount($fileName);
$data['size'] += $subData['size'];
$data['num'] += $subData['num'];
}else{
$data['size'] += filesize($fileName);
$data['num']++;
}
}
}
closedir($dir);
return $data;
}

//文件路径
$cachePath = '文件路径';
$data = getFileCacheCount($cachePath);
switch ($data['size']){
case $data['size'] > 1024:
$data['size'] = round($data['size']/1024, 2) . 'KB';
break;
case $data['size'] > 1024*1024:
$data['size'] = round($data['size']/1024/1024, 2) . 'MB';
break;
default:
$data['size'] = $data['size'] . 'B';
};
posted @ 2017-08-01 10:40  py卡卡  阅读(151)  评论(0编辑  收藏  举报