2011-03-16 2 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); 
?> 

안녕하세요,
카운트 파일과 카탈로그에 대한 재귀 함수가 있습니다. 그것은 카탈로그에 대한 결과를 반환합니다.
하지만 일반 결과가 필요합니다. 스크립트를 어떻게 변경합니까?
반환 값 :
카탈로그와 파일이 각각 3 개 있습니다.
카탈로그와 파일이 1 개 있습니다.
2 개의 카탈로그와 14 개의 파일이 있습니다.
원하는 항목 :
카탈로그 2 개와 파일 18 개가 있습니다.
카운트 파일 및 카탈로그에 대한 재귀 함수를 변경하는 방법은 무엇입니까?

+3

주어진 입력과 예상 출력을 조금씩 명확하게해야한다고 생각합니다. 또한 스크립트를 제대로 들여 쓰면 사람들이 읽는 것을 돕는 데 도움이됩니다. – deceze

답변

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

+0

이 경우 두 개의 서로 다른 개수의 구조를 유지하고 구조를 반환해야합니다. –

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 함수는 유사합니다.

관련 문제