Reputation: 1892
To iterate with PHP over files in a directory I do
$it = new FilesystemIterator('/path/of/files/to/iterate/over');
foreach ($it as $fileinfo) {
echo $fileinfo->getFilename();
}
But what is the order here that files are processed? I did some tests but I can't seem to understand the order PHP is using. It's the same order everytime, at least, but it is neither alphabetically, mtime, ctime or atime. I tried this by creating some empty files with different names, they were neither shown in their chronological creation order nor sorted alphabetically.
What order does FilesystemIterator use? And Why? Can I change the order?
Upvotes: 3
Views: 1455
Reputation: 959
As told here DirectoryIterator sort, an iterator iterates. You will need to build an array and sort it.
An iterator is a desing pattern used to traverse elements on a container. More here: Iterator pattern
Upvotes: 3