代码如下,请各位看看,哪出了问题,为何求不出真实的目录大小function bDir($dirPath){
$size=0;
$dir_handle=opendir($dirPath);
while($file=readdir($dir_handle)){
//echo $file."<br/>";
if($file!=".." and $file!="."){
$filePath=$dirPath."/".$file;
if(is_file($filePath)){
//echo $file.'<br>';
$size+=filesize($dirPath)."<br/>";
}else{
bDir($filePath);
}
}
}
return $size;}

解决方案 »

  1.   

    每次递归时,$size都被重新设置为0了,你这样写求出的应该是最后一个文件的大小把size设置为静态的,或写成类成员,每次递归时相加
      

  2.   

    上面的不是在胡扯么你代码错了两处第一: and -> &&第二: $size += bDir($filePath);
      

  3.   

    function bDir($dirPath){
        $size=0;
        $dir_handle=opendir($dirPath);
        while($file=readdir($dir_handle)){
            //echo $file."<br/>";
            if($file!=".." and $file!=".") {
              $filePath = $dirPath."/".$file;
              if(is_file($filePath)){
                //  echo $file.'<br>';
                $size += filesize($filePath)."<br/>";//这里用错了变量
              }else{
                $size += bDir($filePath); //这里少了返回值
              }
           }
        }
        return $size;
    }
      

  4.   

    我新手,想问下:
    if($file!=".." and $file!=".")这句是什么意思,说什么排除两个目录?麻烦解释下
      

  5.   

    function bDir($dirPath){
        $size=0;
        $dir_handle=opendir($dirPath);
        while($file=readdir($dir_handle)){
            //echo $file."<br/>";
            if($file!=".." && $file!=".") {//用&& 代替 and
              $filePath = $dirPath."/".$file;
              if(is_file($filePath)){
                //  echo $file.'<br>';
                $size += filesize($filePath)."<br/>";//这里用错了变量
              }else{
                $size += bDir($filePath); //这里少了返回值
              }
           }
        }
        return $size;
    }
      

  6.   

    我新手,想问下:
    if($file!=".." and $file!=".")这句是什么意思,说什么排除两个目录?麻烦解释下
    当前目录和上层目录的意思