<p>I am using the code below to display all the files from a directory in a drop down menu. Does anyone know how to make this alphabetical? I presume it has something to do with the sort function, I just can't figure out how!</p>

<pre><code>&lt;?php
$dirname = "images/";
$images = scandir($dirname);
$dh = opendir($dirname);

while ($file = readdir($dh)) {
if (substr($file, -4) == ".gif") {
print "&lt;option value='$file'&gt;$file&lt;/option&gt;\n"; }
}
closedir($dh);
?&gt;
</code></pre>

Why are you reading all the filenames using scandir() and then looping through them with the readdir() method? You could just do this:
```
```
<?php

$dirname = "images/";
$images = scandir($dirname);

// This is how you sort an array, see http://php.net/sort
sort($images);

// There's no need to use a directory handler, just loop through your $images array.
foreach ($images as $file) {
    if (substr($file, -4) == ".gif") {
        print "<option value='$file'>$file</option>\n"; }
    }
}

?>
```
```
Also you might want to use
natsort()
, which works the same way as
sort()
but sorts in "natural order". (Instead of sorting as
```
1,10,2,20
```
it will sort as
```
1,2,10,20
```
.)