Chud37
Chud37

Reputation: 5017

FilesystemIterator only returns one file

Why is this only returning one file?

    $iterator = new FilesystemIterator($absoluteDirectoryPath);
    $regexIterator = new RegexIterator($iterator, $filter);
var_dump($regexIterator);

I just need a list of files and I can't see anywhere why this would be returning only one file. Heres the output:

RegexIterator {#7 ▼
  +replacement: null
  innerIterator: RecursiveIteratorIterator {#5 ▼
    innerIterator: RecursiveDirectoryIterator {#4 ▼
      path: "/home/**/**/**/**/**/**/**/home"
      filename: "home.php"
      basename: "home.php"
      pathname: "/home/**/**/**/**/**/**/**/**/home.php"

I've blanked out the path of course. But theres only one entry, home.php, when theres at 4 files in this directory.

Upvotes: 1

Views: 279

Answers (1)

Thomas Sablik
Thomas Sablik

Reputation: 16447

An iterator by definition points to one element. You have to iterate over all elements and store them in a container. You can use iterator_to_array for this:

$iterator = new FilesystemIterator($absoluteDirectoryPath);
$regexIterator = new RegexIterator($iterator, $filter);
var_dump(iterator_to_array($regexIterator));

or you can use a foreach loop to iterate over all elements and dump each element.

Here you can read more about iterators.

Upvotes: 1

Related Questions