Results 1 to 7 of 7
  1. #1
    Junior Member
    Join Date
    Aug 2003
    Posts
    5

    Default Anyone know how to change the order a directory is read?

    Hi.

    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,

    Daniel
    ________
    WASHINGTON DISPENSARIES
    Last edited by dcardena; 03-24-2011 at 04:31 PM.

  2. #2
    Senior Member FZ's Avatar
    Join Date
    May 2003
    Location
    Johannesburg, South Africa
    Posts
    1,024

    Default

    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...
    Fayez Zaheer

  3. #3
    Senior Member
    Join Date
    Mar 2004
    Location
    U.S.A.
    Posts
    220

    Default

    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]:

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

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

  4. #4
    Senior Member jalal's Avatar
    Join Date
    May 2003
    Location
    Germany
    Posts
    1,377

    Default

    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.

  5. #5
    Moderator wildjokerdesign's Avatar
    Join Date
    Jun 2003
    Location
    Kansas City Mo
    Posts
    5,720

    Default

    php also has a sort() function that should alphabitize the array
    Code:
    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.
    Shawn
    Please remember your charity of choice: http://www.redcross.org

    Handy Links: wildjokerdesign.net | Plain Text Editors: EditPlus | Crimson

  6. #6
    Junior Member
    Join Date
    Aug 2003
    Posts
    5

    Default now wrestling with arrays

    yeah I decided it's probably better to just put the returned values into a multidimensional array ie:

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

    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
    Last edited by dcardena; 03-24-2011 at 04:31 PM.

  7. #7
    Moderator wildjokerdesign's Avatar
    Join Date
    Jun 2003
    Location
    Kansas City Mo
    Posts
    5,720

    Default

    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.
    Shawn
    Please remember your charity of choice: http://www.redcross.org

    Handy Links: wildjokerdesign.net | Plain Text Editors: EditPlus | Crimson

Similar Threads

  1. Miva 4 to Miva 5 upgrade problems Did this happen to you?
    By brady9111 in forum Miva Merchant
    Replies: 3
    Last Post: 05-25-2007, 10:11 AM
  2. Replies: 3
    Last Post: 03-15-2007, 08:32 PM
  3. Replies: 2
    Last Post: 02-21-2006, 03:03 PM
  4. Replies: 10
    Last Post: 07-06-2005, 10:53 AM
  5. FTP Change home directory /same different users
    By noobe in forum General Discussion
    Replies: 4
    Last Post: 05-17-2004, 05:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •