PHP recursive directory search using glob for multiple file types

痞子三分冷 提交于 2019-12-20 04:56:14

问题


I have a bunch of folders with these file types spread across them .mp4,.flv,.wmv,.mov

/video/o/y/oyr800/clips/1.flv
/video/p/y/pyr800/clips/1.wmv
/video/q/y/qyr800/clips/1.mp4
/video/51/51.mov
/video/52/52.flv

I tried using this function to list the paths and filenames but it gives me a blank return:

print_r(rglob('{*.mp4,*.flv,*.wmv,*.mov}',GLOBAL_BRACE));

function rglob($pattern, $flags = 0) {
  $files = glob($pattern, $flags); 
  foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) {
    $files = array_merge($files, rglob($dir.'/'.basename($pattern), $flags));
  }
  return $files;
}

Not sure what is wrong.


回答1:


Ended up using this:

print_r(rsearch("C:\wamp\www","/^.*\.(mp4|flv|wmv|mov)$/"));

function rsearch($folder, $pattern) {
  $dir = new RecursiveDirectoryIterator($folder);
  $ite = new RecursiveIteratorIterator($dir);
  $files = new RegexIterator($ite, $pattern, RegexIterator::GET_MATCH);
  $fileList = array();
  foreach($files as $file) {
    $fileList[] = $file[0];
  }
  return $fileList;
}

Works great!




回答2:


Check out this post for example:

http://thephpeffect.com/recursive-glob-vs-recursive-directory-iterator/

Might want to look at the Recursive Directory Iterator as well



来源:https://stackoverflow.com/questions/25257717/php-recursive-directory-search-using-glob-for-multiple-file-types

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!