使用PHP在文件名中使用字符串或模式查找目录中的所有文件 [英] Find all files in directory with string or pattern in filename with PHP

查看:183
本文介绍了使用PHP在文件名中使用字符串或模式查找目录中的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PHP列出目录中的文件(递归或非递归),其中filename与某个模式匹配。我从来没有使用正则表达式,所以你可以提供的任何帮助将是伟大的。我可以搜索返回的文件名的字面检查,但我认为这不是一个好主意:)

I'm trying to list out files in a directory (recursive or non) with PHP where filename matches a certain pattern. I've never been too great with regex so any help you could offer would be great. I could search for a literal check on the filenames returned, but I think that's not such a great idea :)

更新+最终解决方案:1 / 18/2011 @ 8:06 PM

Update + Final Solution: 1/18/2011 @ 8:06 PM

我发现了另一种方式来做我正在寻找的一次,一旦我明白了正则表达式一点。当然,对于正则表达式,我感到非常沮丧,现在我得到一点点赞赏,因为有一位朋友把我放在一边,比在线指南中更简单地解释一些这个问题。

I found another way to do what I was looking for once I understood regex a bit more. Granted I was entirely frustrated about where I was with regex, I get a bit of it now thanks to a friend pulling me aside to explain some of this in simpler terms than I was finding in online guides.

此解决方案基本上检查具有前缀前缀prefixone或prefixtwo的特定图像,同时还验证它是某种类型的图像(jpg,jpeg,png)并匹配以下任何格式。

This solution basically checks for a specific image(s) with a leading prefix of either "prefixone" or "prefixtwo", while also verifying that it's an image of a certain type (jpg, jpeg, png) and matches any of the following formats.

根据您从Wordpress(我正在使用它)传递的lug,,它将与该正则表达式匹配。以下是一个示例列表:

Depending on the slug you passed from Wordpress (where I was using this), it would match with that regular expression. Here is a sample listing:

prefixone.123-abc._tag1.001.jpg
prefixone.345-xyz._tag1.002.jpeg
prefixtwo.123-abc._tag2._tag1.003.jpg
prefixone.123-abc._tag2.004.jpeg
prefixtwo.345-xyz._tag2._tag3._tag1.005.jpg
prefixtwo.123-abc._tag1.001.jpg
prefixone.345-xyz._tag1.001.png
prefixtwo.456-rst._tag1.001.png

所有这些可能已经从我们的opendir()文件列表中返回的文件功能,任何这些可能是一个匹配,如果slug匹配。无论文件名中标记信息的顺序如何。

All of these files that may have potentially been returned in the file listing from our opendir() function, any of these could have been a match if the slug matched. Regardless of the ordering of tagging information in the filename.

我希望这有助于另一个用户正在使用正则表达式。这是一个痛苦的事情,但一旦你了解一些基本的东西,其余的开始迅速到位,开始建立自己的。

I hope this helps another user struggling with regex. It's a pain to get the hang of but once you understand a few fundamental things, the rest starts to rapidly fall into place to start building your own.

代码: p>

Code:

<?php
// create an array to hold directory list
$results = array();

// create a handler for the directory
$directory = $_SERVER['DOCUMENT_ROOT'].'/some/path/to/images/';
$handler = opendir($directory);

// open directory and walk through the filenames
while ($file = readdir($handler)) {

    // if file isn't this directory or its parent, add it to the results
    if ($file != "." && $file != "..") {

        // check with regex that the file format is what we're expecting and not something else
        if(preg_match('#^(prefixone|prefixtwo)[^\s]*\.'.$wordpress-slug.'\.[^\s]+(\.(jpg|jpeg|png))#', $file)) {

            // add to our file array for later use
            $results[] = $file;
        }
    }
}
?>

我实际上并不需要递归,但网上有很多递归的例子,真的是我最担心的。内容隔离模式是此任务的核心,所以上述代码足够。

I didn't actually need recursive for this, but there really are plenty of recursive examples online and that was truly the least of my worries. Content isolation by pattern was the core of this task so the above code sufficed.

Sidenote:

Sidenote:

对于昨天指出接受评论的人,我不知道我错过了,我很抱歉。我有一个糟糕的一天。对不起,如果我似乎抓住任何人关于评论。这是一个很好的社区,我也很乐意回馈我可以在哪里。

To those that pointed out the "accepted comments" yesterday, I had no idea I was missing that and I apologize. I was having a bad day. Sorry if I seemed to snap at anyone about the comments. This is a great community and I'm happy to give back where I can, also.

推荐答案

使用 glob 找到匹配模式的路径或一个 GlobIterator

Use glob to find pathnames matching a pattern or a GlobIterator.

如果你需要递归使用 RegexIterator 和一个 RecursiveDirectoryIterator 。 / a>

If you need that to be recursive use a RegexIterator and a RecursiveDirectoryIterator.

标记此CW,因为问题是一个确定的重复,您可以轻松地找到使用搜索功能的上述所有示例。请这样做。

Marking this CW because the question is a sure duplicate and you can easily find examples for all of the above when using the Search function. Please do so.

这篇关于使用PHP在文件名中使用字符串或模式查找目录中的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆