Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

opendir() sorting problem.

Posted on 2005-03-25
7
Medium Priority
?
1,349 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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

578 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