Dir List Won't Show in Alphabetical Order


I’m trying to get the code to get the filename of all the .wmv files in a folder and link to the page which will show the video. It’s all working fine apart from it is bringing all the videos up in date order instead of alphabetical order. Can anyone help please?

//Open video directory
$dir = dir(“videos”);

//List files in video directory
while (($file = $dir->read()) !== false)

if (eregi(".wmv", $file)) {
$file = str_replace(".wmv", “”, $file);
$file2 = str_replace(" ", “%20”, $file);
echo “<a href=videos/videoshow.php?title=” . $file2 . “>” . $file . “


According to the manual for php

so using the DIR function will not work.

However you might be able to do something with the shell_exec command, however you would have to do a bit more parsing of the information as it will return the data in one string.

for example.
// do an ls command with the switch of -t for sorting by TIME or -rt for REVERSE order Time.
$data = shell_exec(ls -lt);

// Code to Parse $data goes here


Thanks, Peg. I’ll give that a try tonight.

If you were to put them into an array first you could use asort() function to sort the array and then use a for loop to output the array.

This might be easier than shell_exec(), but don’t quote me has I have never used shell_exec. I have however read through files in a directory and used this method to sort them.


I didn’t think of that, but I think you are right as that would be easier.

Sponsor our Newsletter | Privacy Policy | Terms of Service