Skip to content
Advertisement

Files in directory not in order

I use this code to list all files in directory:

$d = dir($FolderToPlay);
while (($file = $d->read()) !== false){
...
...
}
$d->close();

But, the result is not in the numerical order. How can I fix it?

Advertisement

Answer

Get a list of all the filenames, then you can use a sorting function.

$d = glob("$FolderToPlay/*");
natsort($d);
foreach ($d as $file) {
    ...
}
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement