PHP List Directory structure and exclude some directories

前端 未结 8 677
南笙
南笙 2020-12-18 05:44

I have this PHP Code:

$rootpath = \'../admin/\';
$inner = new RecursiveDirectoryIterator($rootpath);
$fileinfos = new RecursiveIteratorIterator($inner);

for         


        
相关标签:
8条回答
  • 2020-12-18 06:21

    I suggest using the RecursiveCallbackFilterIterator.

    $directory = '../admin/';
    $filter = array('.git');
    
    $fileinfos = new RecursiveIteratorIterator(
      new RecursiveCallbackFilterIterator(
        new RecursiveDirectoryIterator(
          $directory,
          RecursiveDirectoryIterator::SKIP_DOTS
        ),
        function ($fileInfo, $key, $iterator) use ($filter) {
          return $fileInfo->isFile() || !in_array($fileInfo->getBaseName(), $filter);
        }
      )
    );
    
    foreach($fileinfos as $pathname => $fileinfo) {
      //...
    }
    
    0 讨论(0)
  • 2020-12-18 06:21

    I would do it like this:

    $excludes = array(
        'files' => array('file1.ext','file2.ext','fileN.ext'),
        'dirs' => array('dir1','dir2','dirN')
    );
    
    foreach ($directories as $current_dir) {
        if (!in_array($current_dir,$excludes['dirs'])) {
           // directory method
           foreach ($files as $current_file) {
                if (!in_array($current_file,$excludes['files'])) {
                  // files method
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题