Q:

Sort directory list

I would like to sort the output from this script.
It show's an output from a complete directory with some exclusions.
<?php
if ($handle = opendir('.')) {
    $blacklist = array('.', '..', '.svn', 'pagina.php','index.php','index.html');

echo "<h1>Statistics for:</h1>";
echo "<TABLE align=left border=1 cellpadding=5 cellspacing=0 class=whitelinks>";

 while (false !== ($file = readdir($handle))) {
        if (!in_array($file, $blacklist)) {

                echo "<TR><TD><a href='./$file'>$file\n</a></TD></TR>";
        }
    }
    closedir($handle);

echo"</TABLE>";
}
?>

What i would like to do is to sort the output from the directory on the alphabet.
A --> B --> C ...
How can i fix this. I have tried something with sort. But i can get it work

A:

put $file into an array first, use sort on the array then print out the html
$files = array();
while (false !== ($file = readdir($handle))) {
   if (!in_array($file, $blacklist)) {
      $files[] = $file;
   }
}

sort($files,SORT_NATURAL); //use natsort if php is below 5.4

foreach($files as $file) {
   echo "<TR><TD><a href='./$file'>$file\n</a></TD></TR>";
}

