读取文件和目录数都是实际的两倍,包括所计算出来的大小

jackydrl 2007-10-17 06:18:04
<html><head><title>handling dir</title>
<style type="text/css">
body{font-size:14px;}
</style>
<?php
## handle the dir
$fname="../for.php";
//dir
$dir=dirname($fname);
$i=0;
//compute the count of dir and file respectively
$count_dir=0;
$count_file=0;
echo "<h1>Index of ".realpath($dir)."</h1><hr>";
##function list_dir is to list the instant file & sub dir of the directory
function list_dir($dir){
$dh=opendir($dir);
while($file=readdir($dh)){
if($file!="." && $file!=".."){
$elem=$dir."/".$file;
//echo $elem;
//if(filetype($elem)=="dir") //刚开始不知为什么is_dir()不能使用,就用这笨方法了
if(is_dir($elem)){
echo "<img src='folder.gif'> <a href=$dir/$file>".$file."</a></br>";
}
if(is_file($elem)){
echo "<img src='php.gif'> <a href=$dir/$file>".$file."</a></br>";
}
}//end if
}//end while
closedir($dh);
}//end of funciton
##since we have defined the function list_dir,then we call it
list_dir($dir);

##function dsize is to compute the size of the directory
function dsize($dir){
global $count_dir;
global $count_file; //此两变量一定要用global来定义,不能像$totalsize那样定义为一个内部的静态变量,因为 //$totalsize是要被返回的,如果$count_dir和$count_file定义为内部变量的话,没有返回,
//所以就没法访问了
static $totalsize=0; //因为不论是下面的哪一个分支对$totalsize进行处理,都是对同一个$totalsize处理,所以

$dh=opendir($dir); //the data stream opendir() has been closed at the end of function list_dir()
while($file=readdir($dh)){
if($file!="." && $file!=".."){
$elem=$dir."/".$file;
//echo $elem;
if(is_dir($elem)){
$totalsize+=dsize($elem);
$count_dir++;
}
if(is_file($elem)){
$totalsize+=filesize($elem);
$count_file++;
}
}//end if
}//end while
closedir($dh);
return round(($totalsize/1024),2);
}//end of funciton
//echo $i;
dsize($dir);
echo "***---------------------------------------------------------***<br/>";
echo "size of the directory ".realpath($dir)." is ".round((dsize($dir)/2),2)." KB</br>";
echo "count of dir: ".($count_dir/2)."<br/>";
echo "count of file: ".(($count_file/2)-($count_dir/2))."<br/>";
echo "<hr>";
echo "copyright(c) 2007 All Rights Rerserved";
?>
--------------------------------
以上为这个文件的完整源文件,在计算机文件数目和文件夹数目,及大小的时候,都要除以2才是正确的结果,不知为何,请教!
...全文
71 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
jackydrl 2007-10-23
  • 打赏
  • 举报
回复
呵呵,楼上说的对,这个函数调用了两次!不好意思啊,这个问题其实前几天已经被自己发现了,只是好长时间没来论坛了,所以劳大家费心了,谢谢!
Meteorlet 2007-10-22
  • 打赏
  • 举报
回复
dsize($dir); //第一次调用dsize()
echo "***---------------------------------------------------------*** <br/ >";
echo "size of the directory ".realpath($dir)." is ".round((dsize($dir)/2),2)." KB </br >"; //第二次调用dsize()

因为你dsize()调用了两次!
yzxlyd 2007-10-22
  • 打赏
  • 举报
回复
最简单的读取文件大小方法

<?php
$filename = 'note.php';
echo sprintf("%u字节", filesize($filename));
?>

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧