PHP遍历目录并返回统计目录大小

前端之家收集整理的这篇文章主要介绍了PHP遍历目录并返回统计目录大小前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

<div class="codetitle"><a style="CURSOR: pointer" data="79725" class="copybut" id="copybut79725" onclick="doCopy('code79725')"> 代码如下:

<div class="codebody" id="code79725">
<?PHP
$dirname = "test1";
//mkdir($dirname); //遍历一层目录
function listdir($dirname) {
$ds = opendir($dirname);
while($file = readdir($ds)) {
$path = $dirname.'/'.$file;
if(is_dir($file)) {
echo "DIR:".$file."
";
if($file != "." && $file != "..") {
listdir($file);
}
}
else {
echo "FILE:".$file . "
";
}
}
} function totdir($dirname) { //对listdir稍加修改
static $tot = 0;
$ds = opendir($dirname);
while($file = readdir($ds)) {
$path = $dirname.'/'.$file;
if(is_dir($file)) {
//echo "DIR:".$file."
";
if($file != "." && $file != "..") {
$tot += totdir($file);
}
}
else {
//echo "FILE:".$file . "
";
$tot += filesize($path);
}
} //返回总计
return $tot;
} listdir($dirname); echo totdir($dirname)." bytes"; ?>

猜你在找的PHP相关文章