title: Sort and display directory list alphabetically using opendir() in php
body: <p>php noob here - I've cobbled together this script to display a list of images from a folder with opendir, but I can't work out how (or where) to sort the array alphabetically</p>
  
  <pre><code>&lt;?php
  
  // opens images folder
  if ($handle = opendir('Images')) {
  while (false !== ($file = readdir($handle))) {
  
  // strips files extensions  
  $crap   = array(".jpg", ".jpeg", ".JPG", ".JPEG", ".png", ".PNG", ".gif", ".GIF", ".bmp", ".BMP", "_", "-");    
  
  $newstring = str_replace($crap, " ", $file );   
  
  //asort($file, SORT_NUMERIC); - doesnt work :(
  
  // hides folders, writes out ul of images and thumbnails from two folders
  
      if ($file != "." &amp;&amp; $file != ".." &amp;&amp; $file != "index.php" &amp;&amp; $file != "Thumbnails") {
      echo "&lt;li&gt;&lt;a href=\"Images/$file\" class=\"thickbox\" rel=\"gallery\" title=\"$newstring\"&gt;&lt;img src=\"Images/Thumbnails/$file\" alt=\"$newstring\" width=\"300\"  &lt;/a&gt;&lt;/li&gt;\n";}
  }
  closedir($handle);
  }
  
  ?&gt;
  </code></pre>
  
  <p>Any advice or pointers would be much appreciated!</p>