Random images from folder

H2OSX

iKnow (sometimes)
Does anyone know how to write a code for a web site that would randomly pick and show a file (in this case a picture) from a folder on the site as well like pics/homepage/filename.jpg If you know how please share the wealth of your knowlege with me :)
 
This displays a random image on a web page. Images are selected from a folder of your choice.

Create a file that you call "randomimage.php".

Put this code inside that file:

<?php

/*
* Name your images 1.jpg, 2.jpg etc.
*
* Add this line to your page where you want the images to
* appear: <?php include "randomimage.php"; ?>
*/

// Change this to the total number of images in the folder
$total = "11";

// Change to the type of files to use eg. .jpg or .gif
$file_type = ".jpg";

// Change to the location of the folder containing the images
$image_folder = "images/random";

// You do not need to edit below this line

$start = "1";

$random = mt_rand($start, $total);

$image_name = $random . $file_type;

echo "<img src=\"$image_folder/$image_name\" alt=\"$image_name\" />";

?>

That's the code. On every page where you want a random image, you just insert "<?php include 'randomimage.php'; ?>" (without the double quotes) instead. You don't have to put it in an image tag, the script will create the img tag for you (look at the last line in the code).
 
Back
Top