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 url 加密解密函数代码

复制代码 代码如下: $returnUrl = rawurlencode(base64_encode($returnUrl)); //编码 $returnUrl = parse_str(...

PHP检测数据类型的几种方法(总结)

在JavaScript中,使用typeof可以检测基本数据类型,使用instanceof可以检测引用数据类型。在PHP中,也有检测数据类型的方法,具体如下: 1、输出变量的数据类型(ge...

php常用经典函数集锦【数组、字符串、栈、队列、排序等】

php常用经典函数集锦【数组、字符串、栈、队列、排序等】

本文实例总结了php常用经典函数。分享给大家供大家参考,具体如下: 数组函数 数组操作 range(start,end) 创建一个数组,值从start开始,一直到end结束 如果r...

ThinkPHP中调用PHPExcel的实现代码

核心代码: //引入PHPExcel vendor('PHPExcel.PHPExcel'); // Create new PHPExcel object $objPHPExcel...

PHP setcookie设置Cookie用法(及设置无效的问题)

结果碰到一个问题,setcookie设置了Cookie并没有生效,在浏览器端也没有看到。查了一下,原来是setcookie是通过HTTP请求响应的Header来完成的,需要在请求响应内容...