2016-10-31 9 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); 
    } 
} 
関連する問題