php list in array files in dir and sub dirs etc. that match pattern.
I'm looking to list all the files in a dir and its sub dirs that match a pattern and return开发者_开发知识库 the list as an array for later use. I would actually like to pass an array of dirs to do this too.
$dirs= new ArrayIterator(array('./', '/dir_two'));
$list = array();
foreach($dirs as $dir){
$dir_iterator = new RecursiveDirectoryIterator($dir);
$iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
foreach($iterator as $file){
if(preg_match('~.*~',$file->getFilename()))
array_push($list,$file->getPathname());
}
}
print_r($list);
There's glob, but it might be that the patterns do not have enough expressive power for your needs. It uses the same patterns that are used by the shell, not regular expressions.
精彩评论