Closed Thread Icon

Topic awaiting preservation: PHP - Sorting files in directory listing (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=12782" title="Pages that link to Topic awaiting preservation: PHP - Sorting files in directory listing (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: PHP - Sorting files in directory listing <span class="small">(Page 1 of 1)</span>\

 
Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 06-12-2003 15:07

Greetings!

I'm using this:

code:
$path = ".";
$dir_handle = @opendir($path);

while ($file = readdir($dir_handle)) {
if ((substr($file, -3) == "php")&&($file != "index.php")&&($file != "index2.php")&&($file != "000-00-0000.php")&&($file != "comments.php")){
echo "<a href=$path/$file>" . substr(basename($file), 0, 11) . "</a><br />\r\n";
}
}
closedir($dir_handle);



This gives me a listing of some specific files, and works great. But I'd like to have them listed according to file name, (the current listing has no apparent order). What would be the best way to do this?

Petskull
Maniac (V) Mad Scientist

From: 127 Halcyon Road, Marenia, Atlantis
Insane since: Aug 2000

posted posted 06-12-2003 15:25

*cricket* *cricket*

Emperor
Maniac (V) Mad Scientist with Finglongers

From: Cell 53, East Wing
Insane since: Jul 2001

posted posted 06-12-2003 15:39

Pugzly: I'd probably store them in an array, use sort() and then run through the array and pump the results out.

___________________
Emps

FAQs: Emperor

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 06-12-2003 15:51

Yep, that's just what I did.

code:
$path = ".";
$dir_handle = @opendir($path);

while ($file = readdir($dir_handle)) {
$files[] = $file;
}
closedir($dir_handle);
sort($files);
$i=0;
while ($i<count($files)){
if ((substr($files[$i], -3) == "php")&&($files[$i] != "index.php")&&($files[$i] != "index2.php")&&($files[$i] != "000-00-0000.php")&&($files[$i] != "comments.php")){
echo "<a href=$path/$files[$i]>" . $files[$i] . "</a><br />\n";
$i++;
}
}



I'm sure I'll clean it up a little, but this seems to work.

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 06-12-2003 18:56

yeah, sort() will do that.
And it's reasonably efficent as well.
I'd tell it that it's sorting strings, though.
(Look up the parameter yourself... I'm to lazy, and promised a tutorial later tonight that's yet to be written)

« BackwardsOnwards »

Show Forum Drop Down Menu