Call us Toll-Free:
Email us


Get Image List rand image display

Code Wizard, 12-30-2006

This function will take a random image from a directory and display it in your webpage. It can be modified to do many other useful things and list various other file types besides just images. Easily configurable and very easy to setup.

This is being updated, have learned quite a bit since making. Will keep you posted :

philip olson :


function Get_Image_list($dir) {
if(!$dir) {
$dir = ".";
$file_array = array();
$dir_handle = opendir($dir);
$a = 0;
while($file = readdir($dir_handle)) {
if((preg_match('/jpg/',$file)) || // can add more here
(preg_match('/png/',$file)) || // or take some away
(preg_match('/gif/',$file)) ||
(preg_match('/jpeg/',$file))) {

$file_array[$a] = $file;

return $file_array;

$files = Get_Image_list($dir);

$max = count($files)-1;
$index = rand(0,$max);
$image_name = $files[$index];


Display Image :
filename : display.php

$dir = "images/"; // where your images are
include "";

<img border=0 alt="random image" src="<? echo "$dir/$image_name";?>">
Enjoyed this post?

Subscribe Now to receive new posts via Email as soon as they come out.

Post your comments

Note: No link spamming! If your message contains link/s, it will NOT be published on the site before manually approved by one of our moderators.

About Us  |  Contact us  |  Privacy Policy  |  Terms & Conditions