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

yipeiwu_com6年前PHP代码库
复制代码 代码如下:

<?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."<br>";
if($file != "." && $file != "..") {
listdir($file);
}
}
else {
echo "FILE:".$file . "<br>";
}
}
}

function totdir($dirname) { //对listdir稍加修改
static $tot = 0;
$ds = opendir($dirname);
while($file = readdir($ds)) {
$path = $dirname.'/'.$file;
if(is_dir($file)) {
//echo "DIR:".$file."<br>";
if($file != "." && $file != "..") {
$tot += totdir($file);
}
}
else {
//echo "FILE:".$file . "<br>";
$tot += filesize($path);
}
}

//返回总计
return $tot;
}

listdir($dirname);

echo totdir($dirname)." bytes";

?>

相关文章

Fatal error: session_start(): Failed to initialize storage module: files问题解决方法

之前编译安装的LNMP环境+phpmyamdin4.02的版本,今天突然出现这个问题:复制代码 代码如下:Fatal error: session_start(): Failed to...

php无限分类且支持输出树状图的详细介绍

php无限分类且支持输出树状图的详细介绍

复制代码 代码如下:<?php/*** 通用的树型类,可以生成任何树型结构*/class tree{    /**   ...

Eclipse PHPEclipse 配置的具体步骤

最近偶来兴致趁着有些时间,看了看php的书。 说到php就不得不提php的开发环境了,一般的都是采用apache做服务器、mysql做数据库,再加上php组合成一个完备的运行环境,但是好...

PHP防范SQL注入的具体方法详解(测试通过)

一个优秀的PHP程序员除了要能顺利的编写代码,还需要具备使程序处于安全环境下的能力。今天我们要向大家讲解的是有关PHP防范SQL注入的相关方法。 说到网站安全就不得不提到SQL注入(SQ...

分享一则PHP定义函数代码

先贴代码 复制代码 代码如下: <?php     function table(){     &nb...