目录遍历代码

<?
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>

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

posted @ 2011-10-12 21:45  菜菜学习  阅读(327)  评论(0)    收藏  举报