Results 1 to 6 of 6

Thread: Dirs and Files

  1. #1

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Dirs and Files

    I'm running this code:

    PHP Code:
    if ($handle opendir('/home/vbshelf/public_html')) {
        
    $d 0$f 0;
        while (
    false !== ($file readdir($handle))) {
            if (
    $file != "." && $file != "..") {
                if (
    is_dir($file)) {
                    
    $dirs[$d] = $file;
                    
    $d $d 1;
                } else {
                    
    $files[$f] = $file;
                    
    $f $f 1;
                }
            }
        }
        
        
    closedir($handle);
        
    sort($dirs);
        
    sort($files);
        for (
    $i 0$i count($dirs); $i++) {
            echo 
    "<b>" $dirs[$i] . "</b><br>\n";
        }
        for (
    $i 0$i count($files); $i++) {
            echo 
    $files[$i] . "<br>\n";
        }

    This is the only thing I could come up with to sort the directories and files in alphabetical order. Is this the best way to do it, or is there an easier approach?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  2. #2
    Here's my script that I use for my avatars on TT...it just happens to show stuff in alpha order for some reason. I was teh major PHP n00b at the time so ignore the stupidities:
    PHP Code:
    <?
    $basename = basename($PATH_TRANSLATED); 
    $windowtitle = ereg_replace("$basename", "", $PATH_TRANSLATED);
    $current_dir = $windowtitle;

    include('/home/dh2255/header.php');

    if ($current_dir != "/")
    {
        echo "<font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"2\"><a href=\"../\">
                Go up a directory</a></font><br><br>";
    }
    ?>
    <table border="0" cellspacing="0" cellpadding="6">
      <tr bgcolor="#000099"> 
        <td><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2" color="#FFFFFF">File 
          name</font></b></td>
        <td><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2" color="#FFFFFF">Size 
          (bytes)</font></b></td>
        <td><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2" color="#FFFFFF">Last 
          Modified</font></b></td>
      </tr>
      <?
      $bitflip = false;

    $handle = opendir('.');

    while (false !== ($file = readdir($handle)))

        $bitflip = !$bitflip;
        if ($bitflip)
        {
            echo "<tr>";
        }
        else
        {
            echo "<tr bgcolor=\"#EEEEEE\">";
        }
        
        if ($file != "." && $file != "..")
        {
            if (is_dir($file))
            {
                ?><td colspan="3"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><?
                echo "Directory: "<a href=\"$file/\">$file</a>";
                ?></font></td><?
            }
            else
            {
                ?><td><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><?
                echo "<a href=\"$file\">$file</a>";
                
                if (eregi("gif$|png$|jpg$|bmp$|jpg$|jpeg$", $file))
                {
                    echo ":<br><img src=\"$file\">";
                }

                ?></font></td>
                <td><font face="Verdana, Arial, Helvetica, sans-serif" size="2">
                <?
                echo filesize($file);
                ?>
                </font></td>
                <td><font face="Verdana, Arial, Helvetica, sans-serif" size="2">
                <?
                $filemod = filemtime($file);
                echo date("F j, Y h:i:s A", $filemod);
                ?></font></td><?
            }
        }
        ?></tr><?
    } ?>
    </table>
    <?
    include('/home/dh2255/footer.php');
    ?>

  3. #3

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    That doesn't do it in order for me for some reason?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  4. #4
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616

    Re: Dirs and Files

    His will not sort it. There is no algorithm for sorting in his.

    Originally posted by The Hobo
    I'm running this code:

    PHP Code:
    if ($handle opendir('/home/vbshelf/public_html')) {
        
    $d 0$f 0;
        while (
    false !== ($file readdir($handle))) {
            if (
    $file != "." && $file != "..") {
                if (
    is_dir($file)) {
                    
    $dirs[$d] = $file;
                    
    $d $d 1;
                } else {
                    
    $files[$f] = $file;
                    
    $f $f 1;
                }
            }
        }
        
        
    closedir($handle);
        
    sort($dirs);
        
    sort($files);
        for (
    $i 0$i count($dirs); $i++) {
            echo 
    "<b>" $dirs[$i] . "</b><br>\n";
        }
        for (
    $i 0$i count($files); $i++) {
            echo 
    $files[$i] . "<br>\n";
        }

    This is the only thing I could come up with to sort the directories and files in alphabetical order. Is this the best way to do it, or is there an easier approach?
    The only thing you can really change is taking out the $d and $f and the incrementing of those values in the while loop. All you need is [] as that will add the value to the next array.

    -Matt
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  5. #5
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    Originally posted by filburt1
    I was teh major PHP n00b at the time
    you still are aren't you?

  6. #6

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Thanks
    My evil laugh has a squeak in it.

    kristopherwilson.com

Posting Permissions

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



Click Here to Expand Forum to Full Width