Gather image file paths Recursively in PHP(在 PHP 中递归地收集图像文件路径)
问题描述
我正在研究一个相当大的 PHP 类,它通过命令行执行大量图像优化工作,您基本上向程序传递 Image path
或 Folder path
里面有多个图像.然后,它通过最多 5 个其他优化图像的命令行程序运行文件.
I am working on a pretty large PHP class that does a lot of stuff with Image Optimization from the Command line, you basically pass the program an Image path
or a Folder path
that has multiple images inside of it. It then runs the files through up to 5 other command line programs that optimize images.
下面是收集图像路径的循环的一部分,如果路径是文件夹而不是图像路径,它将遍历文件夹中的所有图像并将它们添加到图像数组中.
Below is part of a loop that gathers the images paths, if the path is a Folder instead of an image path, it will iterate over all the images in the folder and add them to the image array.
到目前为止,我已经为 1 个文件夹中的单个图像和图像提供了一切工作.我想修改下面的这一部分,以便它可以递归地深入到 1 个文件夹以获取图像路径.
So far I have everything working for single images and images in 1 folder. I would like to modify this section below so it could recursively go deeper then 1 folder to get the image paths.
有人可以告诉我如何在下面修改它来完成这个吗?
Could someone possibly show me how I could modify this below to accomplish this?
<小时>
更新
@Chris 的回答让我查看了文档,我发现了一个我修改为似乎可行的示例
UPDATE
@Chris's answer got me looking at the Docs and I found an example that I modified to this that seems to work
...
输出只是文件名和这样的路径,这是我的最终目标,到目前为止,这很完美,但如果有更好的方法,我会全力改进
The output is JUST the filenames and there path like this which is my ultimate goal, so far this works perfect but as always if there is a better way I am all for improving
推荐答案
虽然 andreas 的回答可能有效,但你也可以让 PHP 5 的 RecursiveDirectoryIterator 为您完成这项工作并使用更多的 OOP 方法.
While andreas' answer probably works, you can also let PHP 5's RecursiveDirectoryIterator do that work for you and use a more OOP approach.
这是一个简单的例子:
或者,您可以试试这个(从 Zend_Translate_Adapter 复制):
Alternatively, you could try this (copied from Zend_Translate_Adapter):
干杯
克里斯
这篇关于在 PHP 中递归地收集图像文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!