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.