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";

?>

相关文章

php基于curl重写file_get_contents函数实例

本文实例讲述了php基于curl重写file_get_contents函数。分享给大家供大家参考,具体如下: file_get_contents在连接不上的时候会提示Connection...

php中foreach结合curl实现多线程的方法分析

本文实例讲述了php中foreach结合curl实现多线程的方法。分享给大家供大家参考,具体如下: 多线程是php不支持的但我们可以通过foreach来伪多线程了,但这个伪多线程速度不一...

浅谈PHP定义命令空间的几个注意点(推荐)

1.声明命令空间必须是程序脚本的第一条语句。另外,所有非 PHP 代码包括空白符都不能出现在命名空间的声明之前。 下面是错误的示例: <html> <?ph...

当前比较流行的两款PHP加密、解密工具Zend Guard和iconCube介绍

当前比较流行的两款PHP加密、解密工具Zend Guard和iconCube介绍

当前市场上较流行的对PHP进行上述加密授权的软件主要有二种: (1)Zend公司的ZendGuard。 (2)ionCube公司的ionCube PHP Encode。 ZendGuar...

WordPress中限制非管理员用户在文章后只能评论一次

之前有网友提出,在WordPress中有没有办法实现每篇文章只允许用户评论一次? 暂不说这个需求有没有用,毕竟WordPress就是给有各种需求的人用的。这个功能实现起来也比较简单,只需...