I'm have a script that outputs files and folders w/ folders as a header and the files listed below. I'd like to sort them alphabetically, but can't seem to figure it out. I think I need to get them into an array. Or use scandir. Here's the code:

function getDirectory( $path = '.', $level = 0 ){

    $ignore = array( '.', '..', 'index.php' );
    $dh = @opendir( $path );

    while( false !== ( $file = readdir( $dh ) ) ){
        if( !in_array( $file, $ignore ) ){     
            $spaces = str_repeat( ' ', ( $level * 4 ) );
            if( is_dir( "$path/$file" ) ){
                echo "<strong>$spaces $file</strong><br />
";
                getDirectory( "$path/$file", ($level+1) );
            
            } else {
                echo "$spaces <a href=\"$path/$file\">$file</a><br />
";
            }
        }
    }
    
    closedir( $dh );
} 

getDirectory( "." );

Recommended Answers

All 5 Replies

I am surprised that your results are not sorted alphabetically - readdir() always seems to do that for me, on Windows at least. What operating system are you using?

Even I get the sorted result on windows as Nettsite suggests, but would like to suppress the possible warnings on closedir() and readdir() while in attempt to read files as directories, as @closedir() etc

readdir orders by when they were put on the server (As per the php manual). Also, php is server-side, so it shouldn't matter what OS you have (XP, in my case. The site's on a Apache server.) scandir() sorts alphabetically by default, and you can use things like sort, and rsort with it. Like I said in the post, I think I need to use that or somehow get this into an array. I've done that in my trials with this, but can't get them out, formatted to html. Same with scandir. I can't figure out how to split it up right.

...but would like to suppress the possible warnings on closedir() and readdir() while in attempt to read files as directories, as @closedir() etc

I've done that at line four (@opendir). Didn't think I needed to do that with the others. Anyway, here's kind of what I'm getting now:

Folder 2
File 1
File 3
File 4
File 2
Folder 1
File 1
File 2
File 4
File 3
Folder 3
File 2
File 1
File 4
File 3

And that would be the order they went up on the server. So it IS right. But not what I need.

My question about OS referred to server OS, which in the case of the PC i use to develop on, is Windows XP.

You are right about the manual - it does say it returns in the order in which files are stored on the server, which it seems is alphabetical on Windows XP.

I uploaded your script to my Linux server, and hey presto! I have the same result as you get, so I thank you for your question, as you have saved me a lot of potential pain and frustration! I happen to be doing something which depends on files being alphabetically sorted as well at the moment, and I did notice some erratic behaviour when testing on the Linux server that was not ther on the XP server.

Here is my solution, any suggestions for improvement welcome:

<?php

function sortDirectory($path = '.') {
  $ignore = array('.', '..', 'index.php');
  $folder = array();
  $dh = @opendir($path);
  while (false !== ( $file = readdir($dh) )) {
	if (!in_array($file, $ignore)) {
	  if (is_dir("$path/$file")) {
		$folder[$file] = sortDirectory("$path/$file");
	  } else {
		$folder[$file] = "$path/$file";
	  }
	}
  }
  closedir($dh);
  ksort($folder);
  return $folder;
}

function getDirectory($folder = array(),$level = 0) {
  $spaces = str_repeat('&nbsp;', ( $level * 4));
  foreach($folder as $file=>$path) {
	if (is_array($path)) {
	  echo "<strong>$spaces $file</strong><br />";
	  getDirectory($path, $level+1);
	}
	else {
	  echo "$spaces <a href=\"$path\">$file</a><br />";
	}
  }
}

getDirectory(sortDirectory());

?>

Nice! Works great. Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.