Forum Moderators: coopster

Message Too Old, No Replies

ordering directory listings in a certain order

         

surrealillusions

6:43 pm on Dec 3, 2008 (gmt 0)

10+ Year Member



Hi all,

I've got the following code which lists quite simply the folders in a sub-folder.


<?php
getDirectory('testfolder');
function getDirectory( $path = '.', $level = 0 )
{
$ignore = array( '.', '..');
$dh = @opendir( $path );
while( false !== ( $file = readdir( $dh ) ) )
{
if( !in_array( $file, $ignore ) )
{
if(is_dir( "$path/$file" ) )
{
echo "<a href='$path/$file/index.php'>$file</a><br />";
}
}
}
closedir( $dh );
}
?>

However, is it possible to list those folders in alphabetical order? I'm guessing put them into an array of somesort, but im not sure how to do that, and then sort the array using the sort() function, is that the best way of going about it?

Thanks
:)

Little_G

7:59 pm on Dec 3, 2008 (gmt 0)

10+ Year Member



Hi,

If you are using PHP5 then you can use scandir [php.net] which will put the files into an array for you and sort it!

Andrew

surrealillusions

9:13 pm on Dec 3, 2008 (gmt 0)

10+ Year Member



ah..nice..

will give that a bash

:)

surrealillusions

8:30 pm on Dec 4, 2008 (gmt 0)

10+ Year Member



Getting nowhere fast with the scandir() function...i can get it to print the array, but thats about as much as use as a giraffe in a catsuit...

Im stuck with the getting the stuff from that array into some sort of usefulness for the user...

jatar_k

8:44 pm on Dec 4, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



use scandir, which it sounds like you have working, then feed that array into a loop like the one you have above to echo the links