Search code examples
phpsortingopendirscandir

How does PHP readdir/opendir sort


I wondered how come that when calling following code the file(name)s in the array are always sorted differently. How does PHP opendir sort the files and how can I change it in the system without having to put it inside an array first which I then sort?

$dh  = opendir($dir);  
    do {
        $files_in_dir[] = $filename;
    }
    while (false !== ($filename = readdir($dh)));

Solution

  • The entries are returned in the order in which they are stored by the filesystem.

    http://php.net/manual/en/function.readdir.php