PHP List Directory structure and exclude some directories

前端 未结 8 722
南笙
南笙 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:19

    You can use RecursiveFilterIterator to filter dirs and in fooreach loop you have only accepted dirs.

    class MyDirFilter extends RecursiveFilterIterator {
        public function accept() {
    
            $excludePath = array('exclude_dir1', 'exclude_dir2');
            foreach($excludePath as $exPath){
                if(strpos($this->current()->getPath(), $exPath) !== false){
                    return false;
                }
            }
            return true;
    
        }
    }
    
    $rootpath = '../admin/';
    $dirIterator = new RecursiveDirectoryIterator($rootpath);
    $filter   = new MyDirFilter($dirIterator);
    $fileinfos   = new RecursiveIteratorIterator($filter);
    
    foreach($fileinfos as $pathname => $fileinfo)
    {
        // only accepted dirs in loop
    }
    

提交回复
热议问题