问题描述
RecursiveDirectoryIterator是否有任何方法可以基于文件夹而不是一起将子文件夹中的文件回显?
is there any way for RecursiveDirectoryIterator to echo files in subfolders separately based on folder and not all together?
这里是我的示例。我有一个文件夹(事件),其中有多个子文件夹(徽标,人物,乐队)。但是子文件夹的名称在某些事件中会有所不同,因此我不能简单地设置为在这三个文件夹中查找,我需要一个通配符选项。
当我使用RecursiveDirectoryIterator回显这些文件夹中的所有图像时,它可以工作,但是我想根据子文件夹将它们分开,因此它回显以下名称的文件夹名称和所有图像,然后对下一个文件夹重复
Here is my example. I have a folder (event), which has multiple subfolders (logo, people, bands). But subfolder names vary for certain events, so I can't simply set to look inside these three, I need a "wildcard" option.When I use RecursiveDirectoryIterator to echo out all images from these folders, it works, but I would like to separate these based on subfolder, so it echoes out folder name and all images from within below and then repeats for the next folder and so on.
现在我用这个:
<?php
$directory = "path/to/mainfolder/";
foreach (new RecursiveIteratorIterator(new
RecursiveDirectoryIterator($directory,RecursiveDirectoryIterator::SKIP_DOTS)) as $filename)
{
echo '<img src="'.$filename.'">';
}
?>
因此,如何使该回声类似:
徽标
图片,图像,图像
People
图片,图像,图像
...
So, how do I make this echo like:
Logo
image, image, image
People
image, image, image
...
提前感谢任何有用的提示和想法。
Thanks in advance for any useful tips and ideas.
推荐答案
对我来说,放置对象时代码更易读转换为具有描述性名称的变量,然后在实例化其他类时将其传递给它们。我在 RegexIterator() ://php.net/manual/en/class.recursivefilteriterator.php rel = nofollow noreferrer> RecursiveFilterIterator()仅过滤图像文件扩展名(不想扩展 RecursiveFilterIterator()
类)。其余代码很简单,可以迭代,提取字符串和分页。
注意:没有错误处理,最好添加try / catch来管理异常
for me, code is more readable when you put objects into variables with descriptive names then pass those on when instantiating other classes. I've used the RegexIterator() over the RecursiveFilterIterator() to filter just image file extensions (didn't want to get into extending the RecursiveFilterIterator()
class for example). The rest of the code is simple iterating, extracting strings and page breaking.
NOTE: there is no error handling, best to add a try/catch to manage exceptions
<?php
$directory = 'path/to/mainfolder/';
$objRecursiveDirectoryIterator = new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::SKIP_DOTS);
$objRecursiveIteratorIterator = new RecursiveIteratorIterator($objRecursiveDirectoryIterator);
// use RegexIterator() to grab only image file extensions
$objRegexIterator = new RegexIterator($objRecursiveIteratorIterator, "~^.+\.(bmp|gif|jpg|jpeg|img)$~i", RecursiveRegexIterator::GET_MATCH);
$lastPath = '';
// iterate through all the results
foreach ($objRegexIterator as $arrMatches) {
$filename = $arrMatches[0];
$pos = strrpos($filename, DIRECTORY_SEPARATOR); // find position of last DIRECTORY_SEPARATOR
$path = substr($filename, 0, $pos); // path is everything before
$file = substr($filename, $pos + 1); // file is everything after
$myDir = substr($path, strrpos($path, DIRECTORY_SEPARATOR) + 1); // directory the file sits in
if ($lastPath !== $path) { // is the path the same as the last
// display page break and header
if ($lastPath !== '') {
echo "<br />\n";
echo "<br />\n";
}
echo $myDir ."<br />\n";
$lastPath = $path;
}
echo $file . " ";
}
这篇关于RecursiveDirectoryIterator来回显由文件夹分隔的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!