使用 PHP 在任何具有子文件夹的特定文件夹中查找文件
我被困在以下内容中,所以任何帮助将不胜感激。
我有一个如下所示的文件夹树:
images/collections/
在集合文件夹中可能有许多子文件夹
images/collections/collection1, images/collections/collection2
图像的命名方式如下
imageProductCode-something.jpg
我想做的是将一个变量(即我的产品代码)传递给脚本,然后该脚本在任何文件夹中查找文件并返回其路径...
images/collections/collection3/imageProductCode-something.jpg
I am stuck with the following, so any help would be appreciated.
I have a folder tree like follows:
images/collections/
Within the collections folder there could be numerous subfolders
images/collections/collection1, images/collections/collection2
Images are named like
imageProductCode-something.jpg
What i am trying to do is pass one variable which is my product code to a script which then finds the file in any of the folders and returns its path...
images/collections/collection3/imageProductCode-something.jpg
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 RecursiveDirectoryIterator
You can use the RecursiveDirectoryIterator
您可以检查所有集合:
根据目录的大小,创建数据库(平面文件或 MySQL)来查找特定产品代码的图像位置可能会更快。
You could check all your collections:
Depending on the size of your catalogue, it might be faster to create a database (flatfile or MySQL) to lookup the location of the image for a specific product code.
如果您使用的 PHP 版本 < 5.3,您可能需要自己编写递归。
您想要遵循的主要方法:
从图像的根目录开始,检查每个条目。如果是目录,则检查该目录。
看起来工作量很大,所以如果您正在查看大型目录和图像集合,您可能需要研究 unix 命令或 db 链接,如上所述。
另一种方法是将目录逻辑连接到图像,以便您可以根据图像的名称(或其他属性)来判断要在哪个目录中查找。
祝你好运,
伊莎
If you're using a version of PHP which is < 5.3, you might need to write the recursion yourself.
Main methods your would like to follow:
Start at the root of your images, check every entry. If it's a directory, check the directory.
Seems like a lot of work, so if you're looking at large directories and image collections, you might want to look into either the unix command or db linking, as suggested above.
Another way would be to have the directories connected logically to the images, so that you can tell from the name (or other property) of the image in which directory to look.
Good luck,
Yishai