View Full Version : Anyone know how to change the order a directory is read?

03-27-2004, 10:52 AM

I have a script that returns a list of directories and subdirectories, but the order in which the'yre returned seems arbitrary. At first everything was alphabetical except for 3 subdirectories that were in reverse alphabetical. So I started renaming those dirs just to see what would happen, but I couldn't see a pattern- they weren't returning alphabetically, reverse alphabetically, or in order of modification date (or it's reverse). So I started reading a little closer about the method I'm using to read the directory names (via the read property of the PHP dir class) and found this litle tidbit:

from php.net (http://us4.php.net/manual/en/class.dir.php) :

"Note: The order in which directory entries are returned by the read method is system-dependent. "

Great- but how do I control the order... I don't see anything in php.ini relating to order, except for "allow_call_time_pass_reference = Off", I set that to On to no avail.

Any thoughts?

Thanks in advance,

WASHINGTON DISPENSARIES (http://washington.dispensaries.org/)

03-27-2004, 02:04 PM
If you can't find an easier way to do it, you could read the names into an array and then sort that array. I've never played with reading directory contents before, so I have no experience with it, sorry...

03-27-2004, 02:26 PM
I don't know how to read directories in reverse either, but I might personally do what FZ is recommending (in case you ever wanted to NOT read in reverse order).

PHP has array_reverse(), which returns a copy of an array in reverse-sorted order.

Example [1]:

$numbers = range(1,10);
$numbers = array_reverse($numbers);

[1] Example from PHP and MySQL Web Development by Luke Welling and Laura Thomson, 2nd Ed.

03-27-2004, 03:37 PM
You could also use a system call, sort of like this (untested):

exec('ls -t1', $resultarray, $retval)

which gives you all entries in 'earliest first' order in the $resultarray.

03-27-2004, 03:39 PM
php also has a sort() function that should alphabitize the array
sort($directoryarray);You would ofcourse replace $directoryarray with the array your are sorting.

There is also asort() that is used for functions created with a string indes. This retains your string indes values instead of replaceing them by a numerical index. I have a feeling that is what you would want for this.

If you wanted to reverse it you could use rsort() and arsort().

The last way of sorting it ksort() this is going to sort the array using the string indexes.

I have not been able to find anything on the system default. If I find something I'll let you know.

03-27-2004, 04:06 PM
yeah I decided it's probably better to just put the returned values into a multidimensional array ie:

$dirsArray = ['dir1'=>'subdir1','dir2'=>'subdir2','dir3'=>'subdir3'];

then sort the array and return the sorted values.

Now I just have to figure out how to tweak my function to recursively read the dir contents & write found dirs and subdirs to a multidimensional array.

Too much fun!
TT350 (http://www.cyclechaos.com/wiki/Yamaha_TT350)

03-27-2004, 04:34 PM
Ouch! If you happen to have a copy of Beginning PHP4 around chapter 10 File and Directory Handeling may help you out. I takes you through the steps of what I think you are doing. My copies pretty old there may be something more current out. The ISBN number on mine is 1861003730 copyright 2000.