2013-05-03 2 views
0

업로드 할 파일 또는 업로드 할 파일을 확인하는 다른 dir이 포함 된 dir의 그룹을 반복적으로 반복하려고합니다. 지금까지 PHP의 파일 경로 및 재귀

, 나는 깊은 파일 시스템에이 개 수준을 갈 내 스크립트를 받고 있어요,하지만 난 내 함수의 범위 내 현재 전체 파일 경로를 유지하는 방법을 알아 냈하지 않은 :

function getPathsinFolder($basepath = null) { 

    $fullpath = 'www/doc_upload/test_batch_01/'; 

    if(isset($basepath)): 
     $files = scandir($fullpath . $basepath . '/'); 
    else: 
     $files = scandir($fullpath); 
    endif; 

    $one = array_shift($files); // to remove . & .. 
    $two = array_shift($files); 

    foreach($files as $file): 
     $type = filetype($fullpath . $file); 
     print $file . ' is a ' . $type . '<br/>'; 

     if($type == 'dir'): 

      getPathsinFolder($file); 

     elseif(($type == 'file')): 

      //uploadDocsinFolder($file); 

     endif; 

    endforeach; 

} 

을 그래서, 내가 getPathsinFolder라고 부를 때마다 기본 경로가 있습니다. 내가 시작한 디렉토리의 현재 이름을 더하기 시작했습니다. 하지만 그 중간에 중간 폴더가 없습니다. 현재 전체 파일 경로를 범위로 유지하는 방법?

+0

당신은 [를 사용할 수는 RecursiveDirectoryIterator] (http://www.php.net/manual/en/recursivedirectoryiterator.construct.php) 클래스 –

답변

1

매우 간단합니다. 재귀를 원할 경우 getPathsinFolder()를 호출 할 때 매개 변수로 전체 경로를 전달해야합니다.

대형 디렉토리 트리를 검색하면 스택을 사용하여 중간 경로 (일반적으로 힙을 차지함)를 저장하는 것이 훨씬 효율적입니다. 시스템 스택을 많이 사용하지 않고 (경로를 저장할뿐만 아니라 .. 함수 호출의 다음 단계에 대한 전체 프레임

+0

감사합니다. spamsink. 당신의 대답은 나를 도와 줄 것입니다. 아래 수정됩니다. 스택을 살펴 보겠습니다. PHP는 SPL 라이브러리에 SplStack 클래스를 가지고 있지만, 지금까지는 SPL로 많은 것을하지 않았습니다. 또한 RecursiveDirectoryIterator 객체를 사용하는 방법을 생각했지만 적절하게 구현하는 방법이 분명하지 않았습니다. – shotdsherrif

0

네, 함수 내부의 전체 경로를 구축하는 데 필요한 주셔서 감사합니다 여기에 작동하는 버전이다.

function getPathsinFolder($path = null) { 

    if(isset($path)): 
     $files = scandir($path); 
    else: // Default path 
     $path = 'www/doc_upload/'; 
     $files = scandir($path); 
    endif; 

    // Remove . & .. dirs 
    $remove_onedot = array_shift($files); 
    $remove_twodot = array_shift($files); 
    var_dump($files); 

    foreach($files as $file): 
     $type = filetype($path . '/' . $file); 
     print $file . ' is a ' . $type . '<br/>'; 
     $fullpath = $path . $file . '/'; 
     var_dump($fullpath); 

     if($type == 'dir'): 
      getPathsinFolder($fullpath); 
     elseif(($type == 'file')): 
      //uploadDocsinFolder($file); 
     endif; 

    endforeach; 

}