如何从目录迭代器循环中排除文件类型
简单的递归目录迭代器,显示所有文件和目录/子目录。
我没有看到任何内置函数来排除某些文件类型,例如在下面的示例中,我不想输出任何与图像相关的文件,例如 .jpg
、.png 等。我知道有几种方法可以做到这一点,正在寻找最好的建议。
$scan_it = new RecursiveDirectoryIterator("/example_dir");
foreach(new RecursiveIteratorIterator($scan_it) as $file) {
echo $file;
}
Simple directory iterator that is recursive and shows all files and directories/sub-directories.
I don't see any built in function to exclude certain file types, for instance in the following example I do not want to output any image related files such as .jpg
, .png
, etc. I know there are several methods of doing this , looking for advice on which would be best.
$scan_it = new RecursiveDirectoryIterator("/example_dir");
foreach(new RecursiveIteratorIterator($scan_it) as $file) {
echo $file;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
更新:
好吧,所以我是个白痴。 PHP 有一个内置函数:
pathinfo()
试试这个:
原始答案:
为什么不直接在文件名上运行
substr()
看看是否可以它与您要排除的文件类型的扩展名匹配:您可以使用正则表达式使其变得更容易:
您甚至可以使用数组来跟踪文件类型:
Update:
Ok, so I'm an idiot. PHP has a builtin for this:
pathinfo()
Try this:
Original Answer:
Why not just run
substr()
on the filename and see if it matches the extension of the file type you want to exclude:You could make it easier by using regular expressions:
You could even use an array to keep track of your file types:
从 PHP 5.4 开始,可以使用 \RecursiveCallbackFilterIterator
迭代器现在将仅包含 PHP 文件。
From PHP 5.4 can use \RecursiveCallbackFilterIterator
Iterator now will contains only PHP files.
交叉发布原始答案(也在 这个问题) -
将
glob
转换为迭代器似乎比编写自定义FilterIterator
。另请注意,FilterIterator
在迭代过程中仍然会遍历每个项目,只是忽略它,而 glob 似乎不会。 但是,glob
似乎包含而不是排除,因此可能不适合您的场景。无预过滤器:
Glob 预过滤器:
然后,做你的事情:
请注意,在
DirectoryIterator
示例中,$file
将是SplFileInfo
,而glob
示例只是磁盘路径。示例
FilterIterator
扩展用法:
Cross-posting original answer (also in this question) --
Turning
glob
into an iterator seems to prefilter more easily than writing a customFilterIterator
. Also note thatFilterIterator
still steps through each item during iteration, just ignores it, whereas glob doesn't seem to. However,glob
seems to include rather than exclude, so may not fit your scenario.No prefilter:
Glob prefilter:
Then, do your thing:
Note that in the case of the
DirectoryIterator
example,$file
will be an instance ofSplFileInfo
, whereasglob
example is just the disk path.Example
FilterIterator
extensionUsage:
扩展其他答案,您可以通过专门
递归过滤器迭代器
。例如,基于finfo
的方法:同样,如果需要,您可以使用
RecursiveRegexIterator
使用基于文件名的方法。Expanding on the other answers, you could do the filtering in a cleaner way by specialising
RecursiveFilterIterator
. Eg, thefinfo
-based approach:Similarly you could use
RecursiveRegexIterator
if you want to use the filename-based approach.我喜欢示例,这是
我正在寻找一种过滤扩展名的方法。您可以包含/排除扩展名。
$file 将是一个 SplFileInfo 类,因此您可以非常轻松地执行强大的操作:
I love examples and here is one
I was looking a way to filter extension.You can include/ exclude extensions.
$file will be an SplFileInfo class, so you can do powerful stuff really easily:
您可以使用 finfo_file PHP 函数返回有关文件的信息。
代码
输出应该类似于:
编辑
因此您可以使用类似的内容获取非图像文件:
You can use the finfo_file PHP function that returns information about a file.
Code
The output should be something like:
Edit
So you can take the non-image files with something like that:
我就是这样做的:
This is how I would do it: