Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order in FilesystemIterator

http://php.net/manual/en/class.filesystemiterator.php

I noticed that FilesystemIterator returns the files ordered by name. Can anyone confirm this is true and it always happens? I haven't found anything in the docs.

Another question, is there any way to get the files ordered by the creation time on the disk? getCTime() seems to return the change time so I can't use it with usort()

like image 255
Elfy Avatar asked Oct 14 '25 06:10

Elfy


1 Answers

I did some digging into PHP internals.

If I am not mistaken, the __construct method of FileSystemIterator ends up using the VCWD_OPENDIR C macro : https://github.com/php/php-src/blob/2f443acad19816e29b0c944426238d9f23af1ae2/main/streams/plain_wrapper.c#L908

This is a macro to the C Function opendir().

By looking at the documentation of that function, I can't see anything that would allow to define any kind of order : http://pubs.opengroup.org/onlinepubs/009695399/functions/opendir.html

Knowing that, I would assume that the order is not enforced and could vary depending on the type of filesystem used (fat32, ntfs, etc.).

Therefore, If I were you - to be safe - I would implement a PHP function that would order them the way I want.

For your 2nd question, check : PHP: how can I get file creation date?

like image 171
alfallouji Avatar answered Oct 16 '25 18:10

alfallouji