目录遍历代码

<?
function readsfile($path){
$dir=opendir($path);

while ($file=readdir($dir)) {

$filepath=$path."/".$file;
if($file!="."&& $file!=".."){
if(is_dir($filepath)){
echo "<tr>";
echo "<td ><font color='red'>".$file."</font></td><td>".fileNameSize(dirFileSize($filepath))."</td><td>".$time."</td>";
echo "</tr>";
$dirNum++;
}
else{
$time=date("Y-m-d H:i:s", filemtime($filepath));
echo "<tr>";
echo "<td>".$file."</td><td>".fileNameSize(filesize($filepath))."</td><td>".$time."</td>";

$fileNum++;
echo "</tr>";
}

}

}
echo "<tr>";
echo "<td>文件个数".$fileNum."目录个数:".$dirNum."</td>";
}

//统计 目录文件大小涵数
function dirFileSize($dirpath){
$dirSize=0;
$dir=opendir($dirpath);
while ($file=readdir($dir))
{
$filepath=$dirpath."/".$file;
if($file!="."&&$file!=".."){
if(is_dir($filepath)){

$dirSize+=dirFilesize($filepath);//如果是目录递归调用该 函数本身
}
else{
$dirSize+=filesize($filepath); //累加返回文件大小

}
}
}
closedir($dir);
return $dirSize;

}
//计算文件大小转换函数,POW是数学函数,1024等于2的10次方。
function fileNameSize($size){
$dw=Byte;
if($size<0){
$size=0;
}
if($size>Pow(2,10)){
$dw="Kb";
$size=round($size/pow(2,10),2);
}
if($size>Pow(2,20)){
$dw="Mb";
$size=round($size/pow(2,20),2);
}
if($size>Pow(2,30)){
$dw="Gb";
$size=round($size/pow(2,30),2);
}
else{

$dw=Byte;
}
return $size.$dw;
}
$path="../";
echo "<table width=500 spacepadding=0 cellpadding=0><tr><th>文件名</th><th>文件大小</th><th>创建时间</th></tr>";
readsfile($path);
echo "</table>";

?>
<html>
<head>
<style>
table{text-align:center;border:1px solod };
tr{background:#E0E0E0;}
</style>
<body>
</body></head></html>

还有不完善的地方暂时先放着。

转载于:https://www.cnblogs.com/codephp/archive/2011/10/12/2209341.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值