0

I am trying to get a full file structure using the RecursiveDirectoryIterator, RecursiveIteratorIterator and RegexIterator using the below code to get all the .php files in the system:

$directory = new RecursiveDirectoryIterator(realpath("/"));
$iterator = new RecursiveIteratorIterator($directory);
$regex = new RegexIterator($iterator, '/^.+\.php$/i', RecursiveRegexIterator::GET_MATCH);

But when I var_dump $regex, I get this:

object(RegexIterator)[4431]
public 'replacement' => null

What am I doing wrong here? It has me stumped...

NOTE
I got this code from: here

Can O' Spam
  • 2,718
  • 4
  • 19
  • 45
  • 1
    @unixarmy, I wouldn't say this is a duplicate of that as the OP of that question is getting a runtime error where I am not, I am getting it through, just not the information – Can O' Spam Jul 21 '15 at 08:34
  • Yes, $regex is an "Iterator"; you need to iterate over it (using `foreach($regex as $file)` for example) to get the individual values – Mark Baker Jul 21 '15 at 08:37
  • @MarkBaker, I did that just and it worked perfectly, stupid mistake on my part, would you mind putting it as an answer so I can mark as correct as soon as I can? – Can O' Spam Jul 21 '15 at 08:39

1 Answers1

1

$regex is an "Iterator", you need to iterate over it (using foreach($regex as $file) for example) to get the individual values.

You need to iterate/loop over that object - in the same way that you'd iterate over a database resultset - to get the individual values:

foreach($regex as $file) {
    var_dump($file);
}

Note that $file is an array in this case, with a string value (the full filepath) as the only entry

Mark Baker
  • 209,507
  • 32
  • 346
  • 385