Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 944975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:40:51+00:00 2026-05-15T22:40:51+00:00

I have yet to find a good example of how to use the php

  • 0

I have yet to find a good example of how to use the php RegexIterator to recursively traverse a directory.

The end result would be I want to specify a directory and find all files in it with some given extensions. Say for example only html/php extensions. Furthermore, I want to filter out folders such of the type .Trash-0, .Trash-500 etc.

<?php 
$Directory = new RecursiveDirectoryIterator("/var/www/dev/");
$It = new RecursiveIteratorIterator($Directory);
$Regex = new RegexIterator($It,'/^.+\.php$/i',RecursiveRegexIterator::GET_MATCH);

foreach($Regex as $v){
    echo $value."<br/>";
}
?>

Is what I have so far but result in : Fatal error: Uncaught exception ‘UnexpectedValueException’ with message ‘RecursiveDirectoryIterator::__construct(/media/hdmovies1/.Trash-0)

Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T22:40:52+00:00Added an answer on May 15, 2026 at 10:40 pm

    There are a couple of different ways of going about something like this, I’ll give two quick approaches for you to choose from: quick and dirty, versus longer and less dirty (though, it’s a Friday night so we’re allowed to go a little bit crazy).

    1. Quick (and dirty)

    This involves just writing a regular expression (could be split into multiple) to use to filter the collection of files in one quick swoop.

    (Only the two commented lines are really important to the concept.)

    $directory = new RecursiveDirectoryIterator(__DIR__);
    $flattened = new RecursiveIteratorIterator($directory);
    
    // Make sure the path does not contain "/.Trash*" folders and ends eith a .php or .html file
    $files = new RegexIterator($flattened, '#^(?:[A-Z]:)?(?:/(?!\.Trash)[^/]+)+/[^/]+\.(?:php|html)$#Di');
    
    foreach($files as $file) {
        echo $file . PHP_EOL;
    }
    

    This approach has a number of issues, though it is quick to implement being just a one-liner (though the regex might be a pain to decipher).

    2. Less quick (and less dirty)

    A more re-usable approach is to create a couple of bespoke filters (using regex, or whatever you like!) to whittle down the list of available items in the initial RecursiveDirectoryIterator down to only those that you want. The following is only one example, written quickly just for you, of extending the RecursiveRegexIterator.

    We start with a base class whose main job is to keep a hold of the regex that we want to filter with, everything else is deferred back to the RecursiveRegexIterator. Note that the class is abstract since it doesn’t actually do anything useful: the actual filtering is to be done by the two classes which will extend this one. Also, it may be called FilesystemRegexFilter but there is nothing forcing it (at this level) to filter filesystem-related classes (I’d have chosen a better name, if I weren’t quite so sleepy).

    abstract class FilesystemRegexFilter extends RecursiveRegexIterator {
        protected $regex;
        public function __construct(RecursiveIterator $it, $regex) {
            $this->regex = $regex;
            parent::__construct($it, $regex);
        }
    }
    

    These two classes are very basic filters, acting on the file name and directory name respectively.

    class FilenameFilter extends FilesystemRegexFilter {
        // Filter files against the regex
        public function accept() {
            return ( ! $this->isFile() || preg_match($this->regex, $this->getFilename()));
        }
    }
    
    class DirnameFilter extends FilesystemRegexFilter {
        // Filter directories against the regex
        public function accept() {
            return ( ! $this->isDir() || preg_match($this->regex, $this->getFilename()));
        }
    }
    

    To put those into practice, the following iterates recursively over the contents of the directory in which the script resides (feel free to edit this!) and filters out the .Trash folders (by making sure that folder names do match the specially crafted regex), and accepting only PHP and HTML files.

    $directory = new RecursiveDirectoryIterator(__DIR__);
    // Filter out ".Trash*" folders
    $filter = new DirnameFilter($directory, '/^(?!\.Trash)/');
    // Filter PHP/HTML files 
    $filter = new FilenameFilter($filter, '/\.(?:php|html)$/');
    
    foreach(new RecursiveIteratorIterator($filter) as $file) {
        echo $file . PHP_EOL;
    }
    

    Of particular note is that since our filters are recursive, we can choose to play around with how to iterate over them. For example, we could easily limit ourselves to only scanning up to 2 levels deep (including the starting folder) by doing:

    $files = new RecursiveIteratorIterator($filter);
    $files->setMaxDepth(1); // Two levels, the parameter is zero-based.
    foreach($files as $file) {
        echo $file . PHP_EOL;
    }
    

    It is also super-easy to add yet more filters (by instantiating more of our filtering classes with different regexes; or, by creating new filtering classes) for more specialised filtering needs (e.g. file size, full-path length, etc.).

    P.S. Hmm this answer babbles a bit; I tried to keep it as concise as possible (even removing vast swathes of super-babble). Apologies if the net result leaves the answer incoherent.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have yet to find a good reference on this topic. For this example
This is a fairly common problem to which I have yet to find an
I have searched online for a solution, but have yet to find one that
I'm still yet to find a decent solution to my scenario. Basically I have
Does anyone know, or better yet have an example, of a WCF service that
I've yet to find a good resource for debugging RELEASE mode binaries or dumps
I'm working on yet another framework for PHP, and have run into a small
I don't have enough experience with Qt yet to make a good design choice.
Business Objects Web Services returns error codes and I have yet to find a
I have yet another issue which the answer is eluding me. I wish to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.