递归文件搜索 (PHP)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1860393/
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
Recursive File Search (PHP)
提问by Jason
I'm trying to return the files in a specified directory using a recursive search. I successfully achieved this, however I want to add a few lines of code that will allow me to specify certain extensions that I want to be returned.
我正在尝试使用递归搜索返回指定目录中的文件。我成功地实现了这一点,但是我想添加几行代码,允许我指定我想要返回的某些扩展。
For example return only .jpg files in the directory.
例如只返回目录中的 .jpg 文件。
Here's my code,
这是我的代码,
<?php
$it = new RecursiveDirectoryIterator("L:\folder\folder\folder");
foreach(new RecursiveIteratorIterator($it) as $file) {
echo $file . "<br/> \n";
}
?>
please let me know what I can add to the above code to achieve this, thanks
请让我知道我可以在上面的代码中添加什么来实现这一点,谢谢
回答by Jan Han?i?
<?php
$it = new RecursiveDirectoryIterator("L:\folder\folder\folder");
$display = Array ( 'jpeg', 'jpg' );
foreach(new RecursiveIteratorIterator($it) as $file)
{
if (in_array(strtolower(array_pop(explode('.', $file))), $display))
echo $file . "<br/> \n";
}
?>
回答by Greg
You should create a filter:
您应该创建一个过滤器:
class JpegOnlyFilter extends RecursiveFilterIterator
{
public function __construct($iterator)
{
parent::__construct($iterator);
}
public function accept()
{
return $this->current()->isFile() && preg_match("/\.jpe?g$/ui", $this->getFilename());
}
public function __toString()
{
return $this->current()->getFilename();
}
}
$it = new RecursiveDirectoryIterator("L:\folder\folder\folder");
$it = new JpegOnlyFilter($it);
$it = new RecursiveIteratorIterator($it);
foreach ($it as $file)
...
回答by Ben Everard
Try this, it uses an array of allowed file types and only echos out the file if the file extension exists within the array.
试试这个,它使用一组允许的文件类型,并且只在文件扩展名存在于数组中时才回显文件。
<?php
$it = new RecursiveDirectoryIterator("L:\folder\folder\folder");
$allowed=array("pdf","txt");
foreach(new RecursiveIteratorIterator($it) as $file) {
if(in_array(substr($file, strrpos($file, '.') + 1),$allowed)) {
echo $file . "<br/> \n";
}
}
?>
You may also find that you could pass an array of allowed file types to your RecursiveDirectoryIteratorclass and only return files that match.
您可能还会发现,您可以将一组允许的文件类型传递给您的RecursiveDirectoryIterator类,并且只返回匹配的文件。
回答by emix
Let PHP do the job:
让 PHP 来完成这项工作:
$directory = new RecursiveDirectoryIterator('path/to/directory/');
$iterator = new RecursiveIteratorIterator($directory);
$regex = new RegexIterator($iterator, '/\.jpe?g$/i', RecursiveRegexIterator::GET_MATCH);
echo '<pre>';
print_r($regex);
回答by Ahmad ghoneim
Regarding the top voted answer: I created this code which is using fewer functions, only 3, isset(), array_flip(), explode()instead of 4 functions. I tested the top voted answer and it was slower than mine. I suggest giving mine a try:
关于投票最高的答案:我创建了这段代码,它使用较少的函数,只有 3, isset(), array_flip(),explode()而不是 4 个函数。我测试了投票最高的答案,它比我的慢。我建议试试我的:
$it = new RecursiveDirectoryIterator("L:\folder\folder\folder");
foreach(new RecursiveIteratorIterator($it) as $file) {
$FILE = array_flip(explode('.', $file));
if (isset($FILE['php']) || isset($FILE['jpg'])) {
echo $file. "<br />";
}
}
回答by user1158023
You might want to check out this page about using glob() for a similar search:
您可能想查看有关使用 glob() 进行类似搜索的页面:

