本文介绍了PHP获取包含子目录的文件列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试检索目录中的所有图像,包括所有子目录.我目前正在使用
I am trying to retrieve all images in a directory, including all subdirectories. I am currently using
$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
我希望它一次执行整个目录分支,因此结果是:
I want it to do a whole directory branch at a time so the results are:
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
非常感谢您的帮助,欢呼!
Greatly appreciate any help, cheers!
P.S如果我只需将所有子目录放在投资组合下,而不必用通配符具体说明每个目录,那也很好.
P.S It would also be great if I could just get all subdirectories under portfolio without having to specifically state each directory with a wild card.
推荐答案
来自 glob例子
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;
}
}
这篇关于PHP获取包含子目录的文件列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!