|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70734 RecursiveDirectoryIterator output is not sorted
Submitted: 2015-10-18 21:02 UTC Modified: 2015-10-18 23:19 UTC
From: messiah at os dot pl Assigned:
Status: Not a bug Package: *Directory/Filesystem functions
PHP Version: 5.4.45 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: messiah at os dot pl
New email:
PHP Version: OS:


 [2015-10-18 21:02 UTC] messiah at os dot pl
RecursiveDirectoryIterator() with RecursiveIteratorIterator() not sorting all values by name like on Windows. Try script below on Windows and Linux. This should be sorted by default by name folder and subfolder then next filename. Fix this. I tessted this on PHP 5.5.24 Linux and 5.4.8 Linux and 5.6.11 Windows

Test script:
$MójFolder = __DIR__;
$Pliki = array('Pliki'=>array(), 'Foldery'=>array());

$SkanerKatalogówIPlików = new RecursiveDirectoryIterator($MójFolder, RecursiveDirectoryIterator::SKIP_DOTS);
$SkanujWszystkiePodkatalogi = new RecursiveIteratorIterator($SkanerKatalogówIPlików, RecursiveIteratorIterator::SELF_FIRST);

foreach($SkanujWszystkiePodkatalogi as $WszystkiePlikiIFoldery){

        $Pliki['Foldery'][] = $WszystkiePlikiIFoldery .'/';
        $Pliki['Pliki'][] = $WszystkiePlikiIFoldery->getPathname();


return $Pliki;

Expected result:
Add sorting by name example:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2015-10-18 23:19 UTC]
-Summary: Error +Summary: RecursiveDirectoryIterator output is not sorted -Status: Open +Status: Not a bug
 [2015-10-18 23:19 UTC]
The manual does not guarantee anything about the order. RecursiveDirectoryIterator (and the other filesystem based iterators iterate in the order the OS returns the files to them.
That it is sorted on Windows is not guaranteed by PHP, it just seems to happen due to the underlying implementation in the Windows kernel.
 [2015-10-19 07:25 UTC] messiah at os dot pl
So this must be changed. Fix is simple PHP send request filesystem to system, next PHP sorting all files and folders with subfolders then output results to user. This is annoying error(if this is no bug) when You work on Windows and You try start Your app on server(Linux) like I do. Changing OS is not a solution!
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Tue Dec 06 17:05:53 2022 UTC