牛骨文教育服务平台(让学习变的简单)
博文笔记

php统计文件夹所有文件 及 容量大小

创建时间:2016-04-22 投稿人: 浏览次数:178
<?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"; 

?> 

声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。