By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,649 Members | 2,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,649 IT Pros & Developers. It's quick & easy.

beginner question: directory handling & sorting

P: n/a
I am trying to open a directory on a file server to populate a drop down
menu. I've been able to do this, but the results are not sorted
alphabetically. How would I go about this? I've looked at the php.net
site on directory functions, but haven't found a solution that works.
The sort() function seemed like it should, but it instead returned an
error: sort() expects parameter 1 to be array, resource given.

Any help would be appreciated.
Aug 15 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Rik
Eric Capps wrote:
I am trying to open a directory on a file server to populate a drop
down menu. I've been able to do this, but the results are not sorted
alphabetically. How would I go about this? I've looked at the php.net
site on directory functions, but haven't found a solution that works.
The sort() function seemed like it should, but it instead returned an
error: sort() expects parameter 1 to be array, resource given.

Any help would be appreciated.
0. you can't sort files, only the list of files.
1. read all the contents of the directory in an array.
2. sort the array

Grtz,
--
Rik Wasmus
Aug 15 '06 #2

P: n/a

Eric Capps wrote:
I am trying to open a directory on a file server to populate a drop down
menu. I've been able to do this, but the results are not sorted
alphabetically. How would I go about this? I've looked at the php.net
site on directory functions, but haven't found a solution that works.
The sort() function seemed like it should, but it instead returned an
error: sort() expects parameter 1 to be array, resource given.

Any help would be appreciated.
glob() returns a list of file paths already sorted. See
http://fi.php.net/glob/. The elements will include the directory if one
is specified in the pattern. Use basename() to strip it out.

Aug 16 '06 #3

P: n/a
This is a homebrewed function I use to keep track of the size of my
code on certain projects. It takes two arguments: $dir is a string,
the path relative to the webserver of the directory you'd like to
parse, and $exclude is an array of top-level folders you don't want
searched. Because of the way PHP handles directory traversing (at
least on Windows boxes), the list comes back absolutely alphabetically
sorted, like this:

/a/b/c/d/e
/a/c/a/a/a
/b/a

function ls( $dir, $exclude ) {
static $i = 0;
$files = array();
$d = opendir( $dir );
while ($file = readdir($d))
{
if ($file == '.' || $file == '..') continue;
if (in_array($file, $exclude) ) continue;
if (is_dir( $dir.'\\'.$file ) and !in_array( $file, $exclude) ) {
$files += ls( $dir.'\\'.$file );
continue;
}
$files[ $dir."\\$file" ][ "lines" ] = count( file( $dir."\\$file" )
);
$files[ $dir."\\$file" ][ "size" ] = filesize( $dir."\\$file" );
$files[ $dir."\\$file" ][ "name" ] = $file;
$path = $dir."\\$file";
$path = str_replace( "\\", "/", $path );
$path = str_replace( $_SERVER['DOCUMENT_ROOT'], "", $path );
$files[ $dir."\\$file" ][ "path" ] = str_replace( "//", "/", $path );
}
return $files;
}
>From there, it wouldn't be too difficult to work up a solution to sort
by the filename provided by $ls_results[ $iterator ][ 'name' ].

Aug 17 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.