Jump to content

*solved* Sort files listed from directory


Fearpig

Recommended Posts

Hello,  :)
Could someone point me to a tutorial on how to list a folders contents in a descending order?
I've got the code below that creates a list of links to all of the files in the specified folder and now I want to put these in a descending order by filename.


[code]
$path =  "D:\\Intranet v3\TEST\StaffNotice"; 
$dir_handle  =  @opendir($path)  or  die("Unable  to  open  $path"); 

while  ($file  =  readdir($dir_handle))  { 

if($file  ==  "."  ||  $file  ==  ".."  ||  $file  ==  "index.php") 
continue; 
echo  "<a href='$file'>$file</a><br>"; 


closedir($dir_handle); 
[/code]

Thank you!
Link to comment
Share on other sites

[quote author=Gruzin link=topic=107888.msg433334#msg433334 date=1158147727]
Offtopic: Fearpig, can you please tell me how did you change the topic title?
Thank you.
[/quote]
It was me that changed the topic title, if you are wondering how *solved* got added into the topic title. I used the inline thread title editor.
Link to comment
Share on other sites

Thanks Wildteen.....

Huggiebear, here you go.............

[code]
$thefiles = array();
//initialize array
if ($handle = opendir('.')) {
   while (false !== ($file = readdir($handle))) {
       if ($file != "." && $file != ".." && $file != "index.php" && $file != "test.php" && !is_dir($file)) {
            //ignore some of the system files in that folder
            //use filesystem functions to get the filesize and other attributes RTFM
            $thefiles[] = array('filename' => $file);
            //used this way, it just adds the new item to the end of the array
            // it is creating an array of arrays
       }
   }
   closedir($handle);
}
//at this point the $thefiles array contains all the file info
//access it using 2 indexes like this echo $thefiles[0]['filetype'];
//use array functions to sort $thefiles array to your pleasure RTFM
//use the foreach() to cycle through the $thefiles array and echo the files and other attributes out
krsort($thefiles);

foreach ($thefiles as $singlefile) {
    $Doc = $singlefile['filename'];
    echo "<a class='Body2' href='$Doc'>$Doc</a><br>";
}
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.