Jump to content

Archived

This topic is now archived and is closed to further replies.

Fearpig

*solved* Sort files listed from directory

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!

Share this post


Link to post
Share on other sites
Offtopic: Fearpig, can you please tell me how did you change the topic title?
Thank you.

Share this post


Link to post
Share on other sites
goto your first post on a page you psted then click modify and alter the topic title.

good luck.

Share this post


Link to post
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.

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites

×

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.