?
Solved

opendir() sorting problem.

Posted on 2005-03-25
7
Medium Priority
?
1,332 Views
Last Modified: 2006-11-17
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
0
Comment
Question by:sam85281
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 27

Accepted Solution

by:
Diablo84 earned 2000 total points
ID: 13631447
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
 
LVL 16

Author Comment

by:sam85281
ID: 13631525
Worked perfect Diablo84!  Thanks!
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 13631565
no problem :)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:ZhaawZ
ID: 13633650
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
 
LVL 27

Expert Comment

by:Diablo84
ID: 13633687
ZhaawZ, please don't encourage point hungry behaviour.
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 13654928
Hi sam85281,

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

Thanks,

Diablo84
0
 
LVL 16

Author Comment

by:sam85281
ID: 13655443
Wow.  I thought I did close it!  Closing now....
0

Featured Post

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question