title: Load Random Images from Directory
body: <p>I'd like to randomly load images from a directory and have a button somewhere that refreshes the entire page. Here's the current code I have now:</p>
  
  <pre><code>&lt;?php
  $a = array();
  $dir = '../public/wp-content/uploads/2012/01';
  if ($handle = opendir($dir)) {
    while (false !== ($file = readdir($handle))) {
      if (preg_match("/\.png$/", $file)) $a[] = $file;
      elseif (preg_match("/\.jpg$/", $file)) $a[] = $file;
      elseif (preg_match("/\.jpeg$/", $file)) $a[] = $file;
    }
    closedir($handle);
  }
  
  foreach ($a as $i) {
  
    echo "&lt;img src='" . $dir . '/' . $i . "' /&gt;";
  
  }
  
  ?&gt;  
  </code></pre>
  
  <p>The problem is it loads all 400,000 images at once. I only want 30 to load. 30 random images from the directory. I tried looking up some code such as modifying the above to this:</p>
  
  <pre><code>&lt;?php
  $a = array();
  $dir = '../public/wp-content/uploads/2012/01';
  if ($handle = opendir($dir)) {
    while (false !== ($file = readdir($handle))) {
      if (preg_match("/\.png$/", $file)) $a[] = $file;
      elseif (preg_match("/\.jpg$/", $file)) $a[] = $file;
      elseif (preg_match("/\.jpeg$/", $file)) $a[] = $file;
    }
    closedir($handle);
  }
  
  foreach ($a as $i) {
  
    echo "&lt;img src='" . $dir . '/' . $i . "' /&gt;";
    if (++$i == 2) break;
  
  }
  
  ?&gt;  
  </code></pre>
  
  <p>But it seems to do absolutely nothing.. So if someone can help me get 30 random photos from that directory to load and have some type of reload button, that would be of great help.</p>
  
  <p>Thank you in advance</p>