博主辛苦了,我要打赏银两给博主,犒劳犒劳站长。
【摘要】使用PHP计算指定目录下所有文件的大小的总和,使用的是递归的方法。涉及的PHP函数有:opendir()、close_dir()、is_dir()、is_file()、readdir()、filesize()、file_exists()等。
很多时候我们都需要计算自己网页空间的使用量,需要知道已经占用的空间大小,因此我写了一个递归函数计算指定目录下所有文件的大小总和。
function getFileTotSize($dir,&$count){ if(!isset($count['size'])){$count['size'] = 0;
}
if(!isset($count['dir'])){$count['dir'] = 0;
}
if(!isset($count['file'])){$count['file'] = 0;
}
if(!isset($count['html'])){$count['html'] = 0;
}
if(!isset($count['jpg'])){$count['jpg'] = 0;
}
if(!isset($count['png'])){$count['png'] = 0;
}
if(!isset($count['php'])){$count['php'] = 0;
}
if(!file_exists($dir)){echo "目录不存在";
}else{ if(is_dir($dir)){ if($handle=opendir($dir)){ while($list=readdir($handle)){ if($list != "." && $list != ".."){$path = $dir."/".$list;
if(is_file($path)){$count['file'] += 1;
$count['size'] += filesize($path);
switch(getFileType($path)){case "html":
$count['html'] += 1;
break;
case "jpg":
$count['jpg'] += 1;
break;
case "png":
$count['png'] += 1;
break;
case "php":
$count['php'] += 1;
break;
}
}
if(is_dir($path)){$count['dir'] += 1;
getFileTotSize($path,$count);
}
}
}
}
}
closedir($handle);
}
return $count;
}
/**
* 获取文件类型2
* @param string $filename 文件名称
* @return string 文件类型
*/
function getFileType($filename) {$info = pathinfo($filename);
return strtolower($info['extension']);
}
版权归 马富天个人博客 所有
本文标题:《如何用PHP递归计算目录下所有的文件的大小总和》
本文链接地址:http://www.mafutian.com/39.html
转载请务必注明出处,小生将不胜感激,谢谢! 喜欢本文或觉得本文对您有帮助,请分享给您的朋友 ^_^
顶0
踩0
| 评论审核未开启 |
|
||