php 使用PHP查找文件名中包含字符串或模式的目录中的所有文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4718379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find all files in directory with string or pattern in filename with PHP
提问by Will Ashworth
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 :)
我正在尝试使用 PHP 列出目录(递归或非递归)中的文件,其中文件名与某个模式匹配。我对正则表达式从来都不是太好,所以你能提供的任何帮助都会很棒。我可以搜索返回的文件名的字面检查,但我认为这不是一个好主意:)
Update + Final Solution: 1/18/2011 @ 8:06 PM
更新 + 最终解决方案: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.
一旦我对正则表达式有了更多的了解,我就找到了另一种方法来做我正在寻找的东西。诚然,我对使用正则表达式的情况感到非常沮丧,由于一位朋友把我拉到一边,用比我在在线指南中找到的更简单的术语来解释其中的一些内容,我现在得到了一些。
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. Here is a sample listing:
根据您从 Wordpress 传递的 slug(我使用它的地方),它会与该正则表达式匹配。这是一个示例列表:
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. Regardless of the ordering of tagging information in the filename.
所有这些文件可能已经从我们的 opendir() 函数的文件列表中返回,如果 slug 匹配,其中任何一个都可能是匹配的。无论文件名中标记信息的顺序如何。
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.
对于昨天指出“已接受评论”的人,我不知道我错过了这一点,我深表歉意。我度过了糟糕的一天。对不起,如果我似乎对任何人的评论感到不满。这是一个很棒的社区,我也很乐意尽我所能回馈社会。
采纳答案by Gordon
Use glob
to find pathnames matching a patternor a GlobIterator
.
使用glob
找到匹配的文件路径模式或GlobIterator
。
If you need that to be recursive use a RegexIterator
and a RecursiveDirectoryIterator
.
如果您需要递归使用 aRegexIterator
和 a RecursiveDirectoryIterator
。
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.
标记此 CW 是因为该问题肯定是重复的,并且您可以在使用搜索功能时轻松找到上述所有问题的示例。请这样做。