2016-10-31 3 views
0

문제는 하위 디렉토리에 있습니다. 많은 하위 디렉토리와 하위 하위 디렉토리가 있습니다. 모두 확인해야 할 필요가 있습니다. 누군가 도와 줄 방법을 알고있을 수 있습니다.디렉토리에서 PHP 스캔 디렉토리

내 코드 :

$mainFodlers = array_diff(scandir(self::PROJECT_DIRECTORY, 1), array('..', '.','__todo.txt')); 

foreach ($mainFodlers as $mainFodler) { 

    if (is_dir(self::PROJECT_DIRECTORY . '/' . $mainFodler)) { 

     $subFolders = array_diff(scandir(self::PROJECT_DIRECTORY . '/' . $mainFodler, 1), array('..', '.','__todo.txt', 'share_scripts.phtml')); 

    } else { 

     $extension = $this->getExtension($subFolder); 

     if ($extension == 'phtml') { 

      $file = $subFolder; 

      $fileContent = file_get_contents(self::PROJECT_DIRECTORY . '/views/' . $file, true); 

     } 

    } 

} 

답변

1

정말 효과적으로 응답 할 수 있지만이 recursiveIterator 형 접근 방식을 고려해 볼 수있는 중첩 된 폴더의 문제를 해결하기 어렵다 코드의 최종 결과를 확인할 수 없기 때문에. 다음 코드는 좋은 출발점을 제공해야합니다 - 디렉토리 $dir을 가져 와서 반복합니다.

/* Start directory */ 
$dir='c:/temp2'; 

/* Files & Folders to exclude */ 
$exclusions=array(
    'oem_no_drivermax.inf', 
    'smwdm.sys', 
    'file_x', 
    'folder_x' 
); 

$dirItr = new RecursiveDirectoryIterator($dir); 
$filterItr = new DirFileFilter($dirItr, $exclusions, $dir, 'all'); 
$recItr = new RecursiveIteratorIterator($filterItr, RecursiveIteratorIterator::SELF_FIRST); 


foreach($recItr as $filepath => $info){ 
    $key = realpath($info->getPathName()); 
    $filename = $info->getFileName(); 
    echo 'Key = '.$key . ' ~ Filename = '.$filename.'<br />'; 
} 

$dirItr = $filterItr = $recItr = null; 

지원 클래스

class DirFileFilter extends RecursiveFilterIterator{ 

    protected $exclude; 
    protected $root; 
    protected $mode; 

    public function __construct($iterator, $exclude=array(), $root, $mode='all'){ 
     parent::__construct($iterator); 
     $this->exclude = $exclude; 
     $this->root = $root; 
     $this->mode = $mode; 
    } 

    public function accept(){ 
     $folpath=rtrim(str_replace($this->root, '', $this->getPathname()), '\\'); 
     $ext=strtolower(pathinfo($this->getFilename(), PATHINFO_EXTENSION)); 

     switch($this->mode){ 
      case 'all': 
       return !(in_array($this->getFilename(), $this->exclude) or in_array($folpath, $this->exclude) or in_array($ext, $this->exclude)); 
      case 'files': 
       return ($this->isFile() && (!in_array($this->getFilename(), $this->exclude) or !in_array($ext, $this->exclude))); 
      break; 
      case 'dirs': 
      case 'folders': 
       return ($this->isDir() && !(in_array($this->getFilename(), $this->exclude)) && !in_array($folpath, $this->exclude)); 
      break; 
      default: 
       echo 'config error: ' . $this->mode .' is not recognised'; 
      break; 
     } 
     return false; 
    } 
    public function getChildren(){ 
     return new self($this->getInnerIterator()->getChildren(), $this->exclude, $this->root, $this->mode); 
    } 
} 
관련 문제