Recursive File Search (PHP)

前端 未结 8 923
逝去的感伤
逝去的感伤 2020-11-29 07:57

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

相关标签:
8条回答
  • 2020-11-29 08:35
    <?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";
    }
    ?>
    
    0 讨论(0)
  • 2020-11-29 08:35

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

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

    0 讨论(0)
  • 2020-11-29 08:36

    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)
        ...
    
    0 讨论(0)
  • 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 />";
        }
    }
    
    0 讨论(0)
  • 2020-11-29 08:44

    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);
    
    0 讨论(0)
  • 2020-11-29 08:44

    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";
    }
    
    0 讨论(0)
提交回复
热议问题