Recursive File Search (PHP)

為{幸葍}努か 提交于 2019-11-26 09:47:44

问题


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


回答1:


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



回答2:


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



回答3:


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.




回答4:


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



回答5:


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



回答6:


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

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



来源:https://stackoverflow.com/questions/1860393/recursive-file-search-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!