简体   繁体   English

使用PHP查找文件名中带字符串或模式的目录中的所有文件

[英]Find all files in directory with string or pattern in filename with PHP

I'm trying to list out files in a directory (recursive or non) with PHP where filename matches a certain pattern. 我试图用PHP列出目录中的文件(递归或非),其中filename匹配某种模式。 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 :) 我可以搜索返回的文件名的文字检查,但我认为这不是一个好主意:)

Update + Final Solution: 1/18/2011 @ 8:06 PM 更新+最终解决方案:2011年1月18日晚上8:06

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. 我完全对我在正则表达式中的位置感到沮丧,我现在得到了一些感谢,感谢一位朋友将我拉到一边用比在线指南中找到的更简单的术语来解释一些。

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

Depending on the slug you passed from Wordpress (where I was using this), it would match with that regular expression. 根据您从Wordpress传递的slug(我使用它的地方),它将与该正则表达式匹配。 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

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. 所有这些文件可能已经从我们的opendir()函数返回到文件列表中,如果slug匹配,这些文件中的任何一个都可能匹配。 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. 掌握这一点是一件痛苦的经历,但是一旦你理解了一些基本的东西,其余的就会迅速开始建立你自己的东西。

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: 边注:

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. 这是一个很棒的社区,我很乐意尽我所能回馈。

Use glob to find pathnames matching a pattern or a GlobIterator . 使用glob查找与模式GlobIterator 匹配的路径名

If you need that to be recursive use a RegexIterator and a RecursiveDirectoryIterator . 如果需要递归,请使用RegexIteratorRecursiveDirectoryIterator

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. 标记此CW因为问题确实重复,您可以在使用搜索功能时轻松找到上述所有示例。 Please do so. 请这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM