2011-03-16 71 views
0
<?php 
function scan_dir($dirname) { 
$file_count = 0 ;  
$dir_count = 0 ;  
$dir = opendir($dirname); 
while (($file = readdir($dir)) !== false) { 
if($file != "." && $file != "..") { 
if(is_file($dirname."/".$file)) 
++$file_count; 
if(is_dir($dirname."/".$file)) { 
++ $dir_count; 
scan_dir($dirname."/".$file); 
} 
} 
} 
closedir($dir); 
echo "There are $dir_count catalogues and $file_count files.<br>"; 
} 

$dirname = "/home/user/path"; 
scan_dir($dirname); 
?> 

你好,
我有一个计数文件和目录的递归函数。它返回的结果,每个目录。
但我需要一个共同结果。如何更改脚本?
它返回:
有0目录和3个文件。
共有0个产品目录和1个文件。
共有2个目录和14个文件。
我想:
有2个目录和18个文件。
如何更改计数文件和目录的递归函数?

+3

我认为你需要澄清你给定的输入和预期的输出位。另外,正确缩进你的脚本对于帮助那些试图帮助你阅读的人来说会很好。 – deceze 2011-03-16 07:55:53

回答

2

你可以整理代码很多与RecursiveDirectoryIterator

$dirs = new RecursiveIteratorIterator(
      new RecursiveDirectoryIterator(dirname(__FILE__)) 
     , TRUE); 

$dirsCount = $filesCount = 0; 

while ($dirs->valid()) { 

    if ($dirs->isDot()) { 
     $dirs->next(); 
    } else if ($dirs->isDir()) { 
     $dirsCount++; 
    } else if ($dirs->isFile()) { 
     $filesCount++; 
    } 
    $dirs->next(); 
} 

var_dump($dirsCount, $filesCount); 
1

您可以从每次递归调用返回值,总结这些并返回到它的调用者。

<?php 
function scan_dir($dirname) { 
$file_count = 0 ;  
$dir_count = 0 ;  
$dir = opendir($dirname); 
$sub_count = 0; 
while (($file = readdir($dir)) !== false) { 
if($file != "." && $file != "..") { 
if(is_file($dirname."/".$file)) 
++$file_count; 
if(is_dir($dirname."/".$file)) { 
++ $dir_count; 
$sub_count += scan_dir($dirname."/".$file); 
} 
} 
} 
closedir($dir); 
echo "There are $dir_count catalogues and $file_count files.<br>"; 
return $sub_count + $dir_count + $file_count; 
} 

$dirname = "/home/user/path"; 
echo "Total count is ". scan_dir($dirname); 
?> 

代码会给你每个项目的净数。

+0

如果他们想分辨dirs数量和文件数量之间的区别怎么办? – alex 2011-03-16 08:08:50

+0

在这种情况下,您需要维护具有两个不同计数的结构并返回结构。 – 2011-03-16 10:14:22

0

通过一个简单的修改。只是,例如,保持在一个阵列中的计数,你可以从函数返回加起来,以前的计数,像这样:

<?php 
function scan_dir($dirname) { 
    $count['file'] = 0; 
    $count['dir'] = 0; 
    $dir = opendir($dirname); 
    while (($file = readdir($dir)) !== false) { 
     if($file != "." && $file != "..") { 
      if(is_file($dirname."/".$file)) 
       $count['file']++; 
      if(is_dir($dirname."/".$file)) { 
       $count['dir']++; 
       $counts = scan_dir($dirname."/".$file); 
       $count['dir'] += $counts['dir']; 
       $count['file'] += $counts['file']; 
      } 
     } 
    } 
    closedir($dir); 

    return $count; 
} 

$dirname = "/home/user/path"; 
$count = scan_dir($dirname); 
echo "There are $count[dir] catalogues and $count[file] files.<br>"; 
?> 
0

在我opnion,你应该单独计算文件& 2计数DIR不同的功能。它会澄清事实:

<?php 
function scan_dir_for_file($dirname) { 

    $file_count = 0 ;  

    $dir = opendir($dirname); 

    while (($file = readdir($dir)) !== false) { 
    if($file != "." && $file != "..") { 
     if(is_file($dirname."/".$file)) 
     { 
     ++$file_count; 
     } else { 
     $file_count = $file_count + scan_dir($dirname."/".$file); 
     } 
    } 
    } 

    return $file_count 
} 
?> 

的directory_count功能类似。