title: PHP script to parse directory, list all images and add class="last" to the last image in the directory
body: <p>I'm able to parse through the directory and list all images with any of the functions below. I just need to insert a class="last" attribute into the img tag of the last element in the loop.</p>
  
  <p>Also, which of these functions works best for what I'm trying to do?</p>
  
  <p>Any help much appreciated!</p>
  
  <pre><code>function get_images1() {
  
  $exts = 'jpg jpeg png gif';
  
  $str = ''; $i = -1; // Initialize some variables
  $folder = './wp-content/uploads';
  
  $handle = opendir($folder);
  $exts = explode(' ', $exts);
  while (false !== ($file = readdir($handle))) {
      foreach($exts as $ext) { // for each extension check the extension
          if (preg_match('/\.'.$ext.'$/i', $file, $test)) { // faster than ereg, case insensitive
              //$str .= $file;
              $str .="&lt;img src='wp-content/uploads/". $file ."' alt='" . $file . "' /&gt;";
              //if ($str) $str .= '|';
              ++$i;
          }
      }
  }
  echo $str;
  closedir($handle); // Were not using it anymore
  return $str;
  </code></pre>
  
  <p>}</p>
  
  <pre><code>function get_images2() {
  
  //Open images directory
  $dir = @ opendir("wp-content/uploads/");
  
  //List files in uploads directory
  while (($file = readdir($dir)) !== false)
  {
  if(ereg("(.*)\.(jpg|bmp|jpeg|png|gif)", $file))
      {
      echo '&lt;img src="wp-content/uploads/'. $file .'" alt="" /&gt;';
      }
  }
  closedir($dir);
  }
  
  function get_images3() {
  
  $dir = 'wp-content/uploads/';
  $files = scandir($dir);
  //print_r($files);
  $num = count($files);
  for($n=0; $n&lt;$num; $n++) 
  {
  if(ereg("(.*)\.(jpg|bmp|jpeg|png|gif)", $files[$n]))
      {
      echo '&lt;img src="wp-content/uploads/'. $files[$n] .'" alt="" /&gt;';
      }
  }
  }
  
  function get_images()
  {
  $directory = 'wp-content/uploads/';
  $directory_stream = @ opendir($directory);
  // Display information about the directory stream
  //  print_r ($directory_stream);
  while ($entry = readdir ($directory_stream)) 
      {
      if (! is_file ("$directory/$entry"))
      continue;
      echo '&lt;img src="wp-content/uploads/'. $entry .'" alt="" /&gt;';
      }
  }
  </code></pre>