0

I'm a bit of a beginner in php. I have written a script that lists folders and displays files within them. I can't however figure out how to sort them alphabetialy. Does anyone have a suggestion of solving this? (I hope I don't have to change a lot of code, because I have used this code many places....)

$path ="Prosedyrer";
if ($handle = opendir($path)) 
{
    $blacklist = array('.', '..', 'somedir', 'somefile.php');
    while (false !== ($file = readdir($handle)))
    {
        if (!in_array($file, $blacklist))
        {
            echo "<li>$file</a>\n <ul class=\"sub\">";
        }
    }
}
sonicwave
  • 5,952
  • 2
  • 33
  • 49

2 Answers2

0

This is from the PHP document:

array scandir ( string $directory [, int $sorting_order = SCANDIR_SORT_ASCENDING [, resource $context ]] )

sorting_order By default, the sorted order is alphabetical in ascending order. If the optional sorting_order is set to SCANDIR_SORT_DESCENDING, then the sort order is alphabetical in descending order. If it is set to SCANDIR_SORT_NONE then the result is unsorted.

ttkrpink
  • 109
  • 1
  • 10
0

Use glob(), it's much more convenient and gives you an array right away.

$files = glob('SomePath/*');
$blacklist = array('.', '..', 'somedir', 'index.php');
$files = array_diff($files, $blacklist); // get rid of the blacklisted files
sort($files); // sort them a-z!
foreach ($files as $file) echo "<li>$file</a>\n <ul class=\"sub\">";
Oleg Dubas
  • 2,320
  • 1
  • 10
  • 24