Voting

: max(seven, zero)?
(Example: nine)

The Note You're Voting On

cmanley at example dot com
6 years ago
Beware that DirectoryIterator returns a reference to itself with each iteration instead of a unique new SplFileInfo object as one may first assume.
This is important to know if you want to push the results of an iteration into an array for later processing.
If you do that, then all entries in the array will be the same DirectoryIterator object with the same internal state (most likely invalid - see the valid() method), which is probably not what you want.
So if you do want to store the iteration results in an array, then you must convert them to either plain strings or new SplFileInfo objects first.

$todo = [];
foreach (new DirectoryIterator('/some/dir') as $entry) {
if (!$entry->isFile()) {
continue;
}
#$todo []= $entry; # Don't do this!
$todo []= new SplFileInfo($entry->getFilename()); # Do this!
#$todo []= $entry->getFilename(); # or do this if you're only interested in the file name.
}

<< Back to user notes page

To Top