SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jan 2005
    Location
    manama
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Sort directory listing

    Below is my current code and currently works fine. However, I want to have the output sorted or displayed by filenames.

    Please help. thanks, Nestor

    <?
    $handle = opendir('.');
    while (false !== ($file = readdir($handle)))
    {

    if ($file != '.' && $file != '..' && $file != '.htaccess' && $file != '.htpasswd' && !preg_match("/.php/",$file))
    {
    $num=count($file);
    for ($a=0; $a < $num; $a++)
    {
    $file_size = filesize($file);
    $last_mod = date("m/d/Y G:i:s", filectime($file));
    echo ("<TR><TD bgcolor='ffffff'><A HREF='$file'><h1>$file</h></A></TD>");
    echo ("<TD bgcolor='ffffff'><h1>$file_size</h></TD>");
    echo ("<TD bgcolor='ffffff'><h1>$last_mod</h></TD></TR>");

    }

    }
    }
    closedir($handle);
    echo "</table></td></tr></table>";

    ?>
    Last edited by gpnetin; Jan 7, 2005 at 14:24.

  2. #2
    &lt;!-- Insert thoughts here --&gt; pitcher17's Avatar
    Join Date
    Apr 2004
    Location
    The great white north
    Posts
    293
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could always read the filenames into an array and then sort the array. If you want other information about each filename then either create more arrays or build a two-dimensional array.
    PHP Code:
    <?php
    $dir 
    "/etc/php5/";

    // Open a known directory, and proceed to read its contents
    if (is_dir($dir)) {
       if (
    $dh opendir($dir)) {
           while ((
    $file readdir($dh)) !== false) {
                  
    //$file is the filename
                  
    $fileNameArray[] = $file;
            }
       }
    }
    This gives you the array then you just have to sort it.
    PHP Code:
    $fileNameArray sort($fileNameArray); 
    I believe that should give you what you are looking for...

    ..I reserve the right to say whoops, forgot to test that code....
    The more time I save by not planning and documenting,
    the more time I have left to debug.


  3. #3
    SitePoint Addict launchcode's Avatar
    Join Date
    Dec 2004
    Location
    Bristol, UK
    Posts
    259
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to do as pitcher suggested and read the values into an array that you can sort - the file system will return the files in whatever order it feels like at the time, it isn't always consistent or logical (to us anyway).
    Richard Davey

    Launchcode
    PHP Security Guide. Think your scripts are secure? Think again.


Bookmarks

Posting Permissions

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