libel
libel

Reputation: 3

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?

Upvotes: 0

Views: 57

Answers (2)

Barmar
Barmar

Reputation: 780889

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

$d = glob("$FolderToPlay/*");
natsort($d);
foreach ($d as $file) {
    ...
}

Upvotes: 1

Dennis Kozevnikoff
Dennis Kozevnikoff

Reputation: 2277

The easiest way to list all files in a directory is with scarndir() fuction. You can use it like this:

//Create a variable that contains all your files 
$root = scandir($dir);
//Do something with each value, like push them into an array, do regex etc.
foreach($root as $value){
//...
}

Upvotes: 0

Related Questions