Trying to Alphabetize a list of directories.

Discussion in 'PHP' started by GeorgeB., Sep 3, 2007.

  1. #1
    OK well I have a script that looks in a folder and grabs a list of all the directories and lists them in a menu for me.

    NOTE: I am running PHP4 so I cannot use scandir which would have made this a lot easier.

    Here's my ORIGINAL code:
    $mydir = dir('/path/to/my/content/folders/');
    
    while(($file = $mydir->read()) !== false) {
    
    $upperspace = str_replace('-',' ',$file);
    $upper = ucwords($upperspace);
    
         if(is_dir($mydir->path.$file)  == true && $file != '.' && $file != '..') {
    echo "<li><strong><a href='/graphics/$file'>$upper Comments</a></strong></li>
    ";
    
          } 
    
       }
       $mydir->close();
    
    PHP:
    Here is the code when I try to use sort.
    $mydir = dir('/path/to/my/content/folders/');
    
    $file = array();
    while(($file = $mydir->read()) !== false) {
    
    $upperspace = str_replace('-',' ',$file);
    $upper = ucwords($upperspace);
    
    $file = sort($file);
    
         if(is_dir($mydir->path.$file)  == true && $file != '.' && $file != '..') {
    echo "<li><strong><a href='/graphics/$file'>$upper Comments</a></strong></li>
    ";
    
          } 
    
       }
       $mydir->close();
    PHP:
    Notice I defined $file as an array and added sort() to it inside the while loop. I also tried it inside the if statement as well.

    Either way I get the error telling me that $file must be an array.....

    Suggestions?
     
    GeorgeB., Sep 3, 2007 IP
  2. YoEli

    YoEli Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Heya,

    I believe the problem is that the mydir->read() command is returning a string instead of an array.

    Also adding the sort() inside of the while loop defeats the purpose as it can't possibly know all the possible values to sort each time it outputs a filename.

    I have rewritten your code, to do what I think you wanted. However I was not getting the error your talking about on my system. --But the directory check wasn't working so I was being given files and directories in the list.

    Anyway, hope this helps a bit. :)

    
    <?php
    // Create an array of the directory names.
    $directories = array();
    
    $mydir = dir('/');
    
    while(($file = $mydir->read()) !== false) {
         if(is_dir($mydir->path.$file)  == true && $file != '.' && $file != '..') 
    		 {
    		  // Add the directory to the array.
    			array_push($directories, $file);
         } 
       }
    $mydir->close();
    
    // Sort the array of directories.
    sort($directories);
    
    // Output the links.
    for($i = 0; $i < (count($directories)-1); $i++)
    {
     			 $upperspace = str_replace('-',' ',$directories[$i]);
           $upper = ucwords($upperspace);
    
    			 echo "<li><strong><a href='/graphics/".$directories[$i]."'>$upper Comments</a></strong></li>";
    }	
    ?>
    
    Code (markup):
    Elias York
    Yo! Eli!
     
    YoEli, Sep 3, 2007 IP