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?
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.)
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
RecursiveDirectoryIteratordown to only those that you want. The following is only one example, written quickly just for you, of extending theRecursiveRegexIterator.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 isabstractsince 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 calledFilesystemRegexFilterbut 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).These two classes are very basic filters, acting on the file name and directory name respectively.
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
.Trashfolders (by making sure that folder names do match the specially crafted regex), and accepting only PHP and HTML files.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:
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.