我正在尝试检索目录中的所有图像,包括所有子目录。我目前正在使用
$images = glob("{images/portfolio/*.jpg,images/portfolio/*/*.jpg,images/portfolio/*/*/*.jpg,images/portfolio/*/*/*/*.jpg}",GLOB_BRACE);
这可行,但是结果是:
images/portfolio/1.jpg images/portfolio/2.jpg images/portfolio/subdirectory1/1.jpg images/portfolio/subdirectory1/2.jpg images/portfolio/subdirectory2/1.jpg images/portfolio/subdirectory2/2.jpg images/portfolio/subdirectory1/subdirectory1/1.jpg images/portfolio/subdirectory1/subdirectory1/2.jpg
我希望它一次执行整个目录分支,因此结果是:
images/portfolio/1.jpg images/portfolio/2.jpg images/portfolio/subdirectory1/1.jpg images/portfolio/subdirectory1/2.jpg images/portfolio/subdirectory1/subdirectory1/1.jpg images/portfolio/subdirectory1/subdirectory1/2.jpg images/portfolio/subdirectory2/1.jpg images/portfolio/subdirectory2/2.jpg
非常感谢您的帮助,欢呼!
附注:如果我只需将所有子目录都包含在投资组合下,而不必用通配符具体说明每个目录,那也很好。
从示例
if ( ! function_exists('glob_recursive')) { // Does not support flag GLOB_BRACE function glob_recursive($pattern, $flags = 0) { $files = glob($pattern, $flags); foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) { $files = array_merge($files, glob_recursive($dir.'/'.basename($pattern), $flags)); } return $files; } }