@reg random images

Started on the old forum in July 2011 · view the original in the archive · old links redirect here

#random-images

1 reply · 538 views · started 15 years ago · 1 min read

2 participants
v-eee Newbie · 1 posts #1

hi all... im request random images in directory and sub directory.. thx.

something else Fellow · 2,296 posts #2
<?
 $imglist='';
  //$img_folder is the variable that holds the path to the banner images. Mine is images/tutorials/
// see that you don't forget about the "/" at the end 
 $img_folder = "images/tutorials/";

  mt_srand((double)microtime()*1000);

  //use the directory class
 $imgs = dir($img_folder);

  //read all files from the  directory, checks if are images and ads them to a list (see below how to display flash banners)
 while ($file = $imgs->read()) {
   if (eregi("gif", $file) || eregi("jpg", $file) || eregi("png", $file))
     $imglist .= "$file ";

 } closedir($imgs->handle);

  //put all images into an array
 $imglist = explode(" ", $imglist);
 $no = sizeof($imglist)-2;

 //generate a random number between 0 and the number of images
 $random = mt_rand(0, $no);
 $image = $imglist[$random];

//display image
 echo '<img src="'.$img_folder.$image.'" border=0>';
 ?>

Taken From: PHP print random image from a folder

Looks an old script may need to replace eregi with preg_match (but thats entirely up to you) 😛

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close