sufehmi

show-images-random-order.php

Jun 12th, 2020
46
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. /*
  4.  
  5.  show-images-random-order.php
  6.  
  7.  Show all images in a directory in a random order
  8.  
  9.  Example: when you need to show many logo of important institutions & you want to avoid anyone getting upset over the order of the logo is shown. Yup, real story.
  10.  
  11.  credit : DaveRandom = https://stackoverflow.com/a/12666210/882002
  12. */
  13.  
  14. $dir = $_GET['loc'];
  15. $file_display = array ('jpg', 'jpeg', 'png', 'gif');
  16.  
  17. if (file_exists($dir) == false) {
  18.     echo 'Directory \'', $dir, '\' not found';
  19. } else {
  20.     $dir_contents = scandir($dir);
  21.     shuffle($dir_contents);
  22.  
  23.     echo '<center>';
  24.  
  25.     foreach ($dir_contents as $file) {
  26.         $file_type = strtolower(end(explode('.', $file)));
  27.  
  28.  
  29.         if ($file !== '.' && $file !== '..' && in_array($file_type, $file_display) == true) {
  30.             echo '  <img style="max-width: 200px; height: auto; border-width: 25px;   border-color: white;   border-style: solid;" class="photo" src="', $dir, '/', $file, '" alt="', $file, '" />   ';
  31.         }
  32.     }
  33.     echo '</center>';
  34.  
  35.  
  36. }
  37.  
  38. ?>
RAW Paste Data