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
LVL 16
sam85281Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Diablo84Commented:
What does opendir() sort by as a default????

From: http://us3.php.net/manual/en/function.readdir.php
"The filenames are returned in the order in which they are stored by the filesystem."

A method of sorting:

if ($handle = opendir('audio')) {
 while (false !== ($file = readdir($handle))) {
  if ($file != "." && $file != "..") $thefiles[] = $file;
 }
 closedir($handle);
}

sort($thefiles);

for ($i=0;$i<count($thefiles);$i++) {
echo '&file'.($i+1).'='.$thefiles[$i];
}

echo "&total=$i";
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sam85281Author Commented:
Worked perfect Diablo84!  Thanks!
0
Diablo84Commented:
no problem :)
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

ZhaawZSoftware DeveloperCommented:
For custom sorting by other parameters (file size, creation date etc - whatever you need) You may take a look at
http://www.experts-exchange.com/Web/Web_Languages/PHP/Q_21213726.html
0
Diablo84Commented:
ZhaawZ, please don't encourage point hungry behaviour.
0
Diablo84Commented:
Hi sam85281,

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

Thanks,

Diablo84
0
sam85281Author Commented:
Wow.  I thought I did close it!  Closing now....
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.