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

?>

相关文章

关于url地址传参数时字符串有回车造成页面脚本赋值失败的解决方法

在通过url地址接受参数的时候,有些参数的值V带有回车' %0A ',这时候在页面脚本显示的时候,把这个值V付给脚本变量,可能会造成脚本的错误。 所以,相应的:一开始在传值的时候对一些字...

php简单实现发送带附件的邮件

本文实例讲述了php简单实现发送带附件的邮件。分享给大家供大家参考。具体如下: 下面是静态html代码: <html> <head> <title&...

php设置静态内容缓存时间的方法

本文实例讲述了php设置静态内容缓存时间的方法。分享给大家供大家参考。具体方法分析如下: 在利用百度工具作一个小测试时提示我们需要设置静态内容缓存时间了,我自己没有服务器权限操作,只能从...

php实现比较两个字符串日期大小的方法

本文实例讲述了php实现比较两个字符串日期大小的方法。分享给大家供大家参考。具体如下: <?php function dateBDate($date1, $date2)...

作为PHP程序员你要知道的另外一种日志

作为PHP程序员你要知道的另外一种日志

前言 本来准备讲解nginx和apache的日志的,但是个人不太推荐apache(纯属个人爱好),这里就不介绍apache的日志了。 作为一名程序员,比码代码还重要那么一点点的东西...