递归文件搜索 (PHP)

发布于 2024-08-13 07:20:55 字数 345 浏览 2 评论 0原文

我正在尝试使用递归搜索返回指定目录中的文件。 我成功地实现了这一点,但是我想添加几行代码,以允许我指定我想要返回的某些扩展名。

例如,仅返回目录中的 .jpg 文件。

这是我的代码,

<?php
$it = new RecursiveDirectoryIterator("L:\folder\folder\folder");
foreach(new RecursiveIteratorIterator($it) as $file) {
echo $file . "<br/> \n";
}
?>

请让我知道我可以在上面的代码中添加什么来实现这一点,谢谢

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(9

孤城病女 2024-08-20 07:20:55
<?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";
}
?>
<?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";
}
?>
墨洒年华 2024-08-20 07:20:55

您应该创建一个过滤器:

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)
    ...

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)
    ...
软的没边 2024-08-20 07:20:55

试试这个,它使用允许的文件类型的数组,并且仅在数组中存在文件扩展名时才回显该文件。

<?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";
    }
}
?>

您可能还会发现,可以将允许的文件类型数组传递给 RecursiveDirectoryIterator 类,并且仅返回匹配的文件。

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 RecursiveDirectoryIterator class and only return files that match.

岁吢 2024-08-20 07:20:55

让 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);

Let PHP do the job:

$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);
素罗衫 2024-08-20 07:20:55

关于投票最高的答案:我创建的代码使用的函数较少,只有 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 />";
    }
}

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:

$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 />";
    }
}
洛阳烟雨空心柳 2024-08-20 07:20:55

这些都不适合我的情况。所以我写了这个函数,不确定效率,我只是想快速删除一些重复的照片。希望它对其他人有帮助。

function rglob($dir, $pattern, $matches=array())
{
    $dir_list = glob($dir . '*/');
    $pattern_match = glob($dir . $pattern);

    $matches = array_merge($matches, $pattern_match);

    foreach($dir_list as $directory)
    {
        $matches = rglob($directory, $pattern, $matches);
    }

    return $matches;
}

$matches = rglob("C:/Bridge/", '*(2).ARW');

唯一可以做的一点改进是,目前要使其工作,您必须在起始目录上有一个尾部正斜杠。

None of these worked for my case. So i wrote this function, not sure about efficiency, I just wanted to remove some duplicate photos quickly. Hope it helps someone else.

function rglob($dir, $pattern, $matches=array())
{
    $dir_list = glob($dir . '*/');
    $pattern_match = glob($dir . $pattern);

    $matches = array_merge($matches, $pattern_match);

    foreach($dir_list as $directory)
    {
        $matches = rglob($directory, $pattern, $matches);
    }

    return $matches;
}

$matches = rglob("C:/Bridge/", '*(2).ARW');

Only slight improvement that could be made is that currently for it to work you have to have a trailing forward slash on the start directory.

故笙诉离歌 2024-08-20 07:20:55

在此示例代码中;

  • 您可以为 $directory 变量设置任何路径,例如 ./'L:\folder\folder\folder' 或任何内容。 。
  • 您还可以在 $pattern 中设置模式文件名(可选),您可以为每个带有 .jpg< 的文件添加 '/\.jpg$/' /code> 扩展名,如果你想查找所有文件,可以使用 '//' 作为这个变量。
$directory = 'L:\folder\folder\folder';
$pattern = '/\.jpg$/'; //use "//" for all files
$directoryIterator = new RecursiveDirectoryIterator($directory);
$iteratorIterator = new RecursiveIteratorIterator($directoryIterator);
$regexIterator = new RegexIterator($iteratorIterator, $pattern);
foreach ($regexIterator as $file) {
    if (is_dir($file)) continue;
    echo "$file\n";
}

In this sample code;

  • You can set any path for $directory variable, for example ./, 'L:\folder\folder\folder' or anythings...
  • And also you can set a pattern file name in $pattern optional, You can put '/\.jpg$/' for every file with .jpg extension, and also if you want to find all files, can just use '//' for this variable.
$directory = 'L:\folder\folder\folder';
$pattern = '/\.jpg$/'; //use "//" for all files
$directoryIterator = new RecursiveDirectoryIterator($directory);
$iteratorIterator = new RecursiveIteratorIterator($directoryIterator);
$regexIterator = new RegexIterator($iteratorIterator, $pattern);
foreach ($regexIterator as $file) {
    if (is_dir($file)) continue;
    echo "$file\n";
}
薄暮涼年 2024-08-20 07:20:55

这是在文件夹和子文件夹中搜索的正确方法

$it = new RecursiveDirectoryIterator(__DIR__);
$findExts = Array ( 'jpeg', 'jpg' );
foreach(new RecursiveIteratorIterator($it) as $file)
{        
    $ext = pathinfo($file, PATHINFO_EXTENSION);
    if(in_array($ext, $findExts)){
        echo $file.PHP_EOL;
    }
}

here is the correct way to search in folders and sub folders

$it = new RecursiveDirectoryIterator(__DIR__);
$findExts = Array ( 'jpeg', 'jpg' );
foreach(new RecursiveIteratorIterator($it) as $file)
{        
    $ext = pathinfo($file, PATHINFO_EXTENSION);
    if(in_array($ext, $findExts)){
        echo $file.PHP_EOL;
    }
}
罗罗贝儿 2024-08-20 07:20:55

您可能想查看有关使用 glob() 进行类似搜索的页面:

http: //www.electrictoolbox.com/php-glob-find-files/

You might want to check out this page about using glob() for a similar search:

http://www.electrictoolbox.com/php-glob-find-files/

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