2

在这里,我有一个问题来找出父文件夹的大小:

这是我的示例代码:

function foldersize($dir){
 $count_size = 0;
 $count = 0;
 $dir_array = scandir($dir);
 foreach($dir_array as $key=>$filename){
  if($filename!=".." && $filename!="."){
   if(is_dir($dir."/".$filename)){
    $new_foldersize = foldersize($dir."/".$filename);
    $count_size = $count_size + $new_foldersize[0];
    $count = $count + $new_foldersize[1];
   }else if(is_file($dir."/".$filename)){
    $count_size = $count_size + filesize($dir."/".$filename);
    $count++;
   }
  }

 }

 return $count_size;
}

例子

如果我的文件夹结构如下:

 mm[30MB]->rr[40MB]->ee[300MB]->ss[309MB]

如果我使用上面的代码来查找mm文件夹的大小,它显示的大小为zero

任何人提出一些想法............

4

1 回答 1

2

答案是:

function foldersize($dir){
     $count_size = 0;
     $count = 0;
     $dir_array = scandir($dir);
     foreach($dir_array as $key=>$filename){
      if($filename!=".." && $filename!="."){
       if(is_dir($dir."/".$filename)){
        $new_foldersize = foldersize($dir."/".$filename);
        $count_size = $count_size + $new_foldersize;
       }else if(is_file($dir."/".$filename)){
        $count_size = $count_size + filesize($dir."/".$filename);
        $count++;
       }
      }

     }

     return $count_size;
    }
于 2013-04-25T07:21:10.367 回答