List all images from Directory (and sub-directory)


/ Published in: PHP
Save to your folder(s)

This snippet will create an array of all images in a directory and sub-directories and display them randomly.


Copy this code and paste it in your HTML
  1. <?php
  2. //path to directory to scan. i have included a wildcard for a subdirectory
  3. $directory = "images/*/";
  4.  
  5. //get all image files with a .jpg extension.
  6. $images = glob("" . $directory . "*.jpg");
  7.  
  8. $imgs = '';
  9. // create array
  10. foreach($images as $image){ $imgs[] = "$image"; }
  11.  
  12. //shuffle array
  13. shuffle($imgs);
  14.  
  15. //select first 20 images in randomized array
  16. $imgs = array_slice($imgs, 0, 20);
  17.  
  18. //display images
  19. foreach ($imgs as $img) {
  20. echo "<img src='$img' /> ";
  21. }
  22. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.