Jump to content


Photo

*solved* Sort files listed from directory


  • Please log in to reply
6 replies to this topic

#1 Fearpig

Fearpig
  • Members
  • PipPipPip
  • Advanced Member
  • 195 posts

Posted 13 September 2006 - 11:27 AM

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.


$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);  

Thank you!

"Whats wrong with the cat?" - Mrs Schrödinger

#2 Fearpig

Fearpig
  • Members
  • PipPipPip
  • Advanced Member
  • 195 posts

Posted 13 September 2006 - 11:34 AM

Ignore it again, I found out how!!

Stick it in an array and use krsort($array_name)!

"Whats wrong with the cat?" - Mrs Schrödinger

#3 Gruzin

Gruzin
  • Members
  • PipPipPip
  • Advanced Member
  • 448 posts
  • LocationGeorgia

Posted 13 September 2006 - 11:42 AM

Offtopic: Fearpig, can you please tell me how did you change the topic title?
Thank you.
I don't need your script, I'll try to write it myself

#4 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 13 September 2006 - 11:47 AM

goto your first post on a page you psted then click modify and alter the topic title.

good luck.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#5 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 13 September 2006 - 11:48 AM

Please can you post the solution...

Regards
Rich
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#6 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 13 September 2006 - 12:10 PM

Offtopic: Fearpig, can you please tell me how did you change the topic title?
Thank you.

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.

#7 Fearpig

Fearpig
  • Members
  • PipPipPip
  • Advanced Member
  • 195 posts

Posted 13 September 2006 - 12:14 PM

Thanks Wildteen.....

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

$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>"; 
}

"Whats wrong with the cat?" - Mrs Schrödinger




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users