Link to home
Start Free TrialLog in
Avatar of sam85281
sam85281

asked on

opendir() sorting problem.

First, a question.  What does opendir() sort by as a default????

I have this in a directory:
01_blahblah.mp3
02_blahblah.mp3
03_blahblah.mp3
04_blahblah.mp3
05_blahblah.mp3
06_blahblah.mp3

In the directory it IS sorted correctly.  When I run opendir() it sorts them as: 01, 03, 04, 05, 06, 02.
There is no sense to this.  It's not sorted by date, fileSize, numerically, OR alphabetically.

Obviously I want them to sort numerically.

Here's the code I'm using to get my results:

if ($handle = opendir('audio')) {
$i = 1;
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
echo "&file".$i."=".$file;
$i++;
}
}
closedir($handle);
}
$i--;
echo "&total=".$i;

The results feed into a Flash movie in the format of:
&file1=01_blahblah.mp3&file2=02_blahblah.mp3&file3=03_blahblah.mp3&file4=04_blahblah.mp3&file5=05_blahblah.mp3&file6=06_blahblah.mp3&total=6

It currently all works other than the order being wrong.

How can I get this to order right?

Thanks,
-Sam
ASKER CERTIFIED SOLUTION
Avatar of Diablo84
Diablo84

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sam85281
sam85281

ASKER

Worked perfect Diablo84!  Thanks!
no problem :)
For custom sorting by other parameters (file size, creation date etc - whatever you need) You may take a look at
https://www.experts-exchange.com/questions/21213726/Directory-listing-sort-array-by-filename-and-then-extension-type.html
ZhaawZ, please don't encourage point hungry behaviour.
Hi sam85281,

If you have a free moment could you close this question.

Thanks,

Diablo84
Wow.  I thought I did close it!  Closing now....