Foo Bar
Foo Bar

Reputation: 1892

What determines the order of PHP's FilesystemIterator and can I change it?

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

Answers (1)

Pep Lainez
Pep Lainez

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

Related Questions