使用 PHP 在任何具有子文件夹的特定文件夹中查找文件

发布于 2024-10-03 02:19:08 字数 434 浏览 1 评论 0原文

我被困在以下内容中,所以任何帮助将不胜感激。

我有一个如下所示的文件夹树:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

末蓝 2024-10-10 02:19:08

您可以使用 RecursiveDirectoryIterator

function getImageDirectory($iId) {
    $oDirectory = new RecursiveDirectoryIterator("/path/to/images/");
    $oIterator = new RecursiveIteratorIterator($oDirectory);
    foreach($oIterator as $oFile) {
        if ($oFile->getFilename() == 'imageProductCode-' . $iId . '.jpg') {
            return $oFile->getPath();
        }
    }
}

You can use the RecursiveDirectoryIterator

function getImageDirectory($iId) {
    $oDirectory = new RecursiveDirectoryIterator("/path/to/images/");
    $oIterator = new RecursiveIteratorIterator($oDirectory);
    foreach($oIterator as $oFile) {
        if ($oFile->getFilename() == 'imageProductCode-' . $iId . '.jpg') {
            return $oFile->getPath();
        }
    }
}
泡沫很甜 2024-10-10 02:19:08

您可以检查所有集合:

$collections = array('collection1', 'collection2', 'collection3');

foreach ($collections as $collection) {
  if (file_exists('/path/to/collections/'.$collection.'/'.$productCode.'.jpg')) {
    ... do your thing ...
    break;
  }
}

根据目录的大小,创建数据库(平面文件或 MySQL)来查找特定产品代码的图像位置可能会更快。

You could check all your collections:

$collections = array('collection1', 'collection2', 'collection3');

foreach ($collections as $collection) {
  if (file_exists('/path/to/collections/'.$collection.'/'.$productCode.'.jpg')) {
    ... do your thing ...
    break;
  }
}

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.

痴骨ら 2024-10-10 02:19:08

如果您使用的 PHP 版本 < 5.3,您可能需要自己编写递归。
您想要遵循的主要方法:

  1. opendir
  2. readdir
  3. is_dir
  4. is_file

从图像的根目录开始,检查每个条目。如果是目录,则检查该目录。
看起来工作量很大,所以如果您正在查看大型目录和图像集合,您可能需要研究 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:

  1. opendir
  2. readdir
  3. is_dir
  4. is_file

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文