Posted By

superuntitled on 07/23/09


Tagged

php list image directory


Versions (?)

Who likes this?

5 people have marked this snippet as a favorite

fackz
jealousy
alexteg
bshantz
LeslieOA


List all images from Directory (and sub-directory)


 / Published in: PHP
 

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

  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
Posted By: reptkeu on February 26, 2010

Hello, I can't manage to get this to work, is there a way you could help me? my page is there: bertrandclerc.com/autres/random_images/script2.php Thanks

You need to login to post a comment.