Display a random picture

Back to php.

thibault    (2009-12-16)


Display a random picture

Every webmaster encountered this problem at least one time when coding his pages : How to display a random picture from one folder or even several folders?

Here is a simple way to do it in PHP. I just made this script, so do not hesitate if you have ideas for any improvement. In brief, the script fills an array with every file it founds in the specified folders, then it creates a random number and selects a picture.

Please see the responses to find the script.

(more options below)

thibault    (2009-12-16 13:11:26)


Display a random picture


$pic_directories = array();

$pic_directories[0] = 'images_0';  // Just add other lines for several folders

$pic_image = array();
$pic_directory = array();

foreach($pic_directories as $pic_dir)
if (is_dir($pic_dir))
if ($dh = opendir($pic_dir))
while (($pic_file = readdir($dh)) !== false)
if ($pic_file != '...' && $pic_file != '..' && $pic_file != '.')
$pic_image[] = $pic_file;
$pic_directory[] = $pic_dir;


$pic_total = count($pic_image)-1;
$pic_random = rand(0, $pic_total);

print '<img src="'.$pic_directory[$pic_random].'/'.$pic_image[$pic_random].'" title="'.substr($pic_image[$pic_random],0,strpos($pic_image[$pic_random],'.')).'">';


*rofuka    (2010-06-05 19:29:19)
12 years ago

Thanks, this is very useful!

See also

More websites

You must register to see these links, as this is a collaborative page, then you may change the order of the links by clicking the icons before the titles.

Other websites

The following links might be less relevant, please change their ranks if you find them useful.

 Display Random Image or Quote
javascript.about > library/blrand.htm
  1. Do you have a number of ads that you use on your site
  2. section of your page where you want the link or message
 PHP Random Images | Randomizing Scripts & Programs | Hot Scripts
hotscripts > category/php/scripts programs/randomizing/random images

to create a dynamic link which will rotate the uploaded...

 How to Display a Random Image
mediacollege > internet/javascript/image/random

to reflect the total number of images (remember to count the...

 WP Widgets » blog.oddy.net
blog.oddy > wordpress widgets

November 25th, 2006 at 10:29 pm...
December 14th, 2007 at 12:18 pm...

 A List Apart: Articles: Random Image Rotation
alistapart > articles/randomizer

Dan Benjamin is a business strategist, programmer, writer, and...

 displaying random images on web page using javascript - random image display on web page
webdevelopersnotes > tutorials/javascript/random image

Displaying random images on web page using javascript - random image...
Embedding JavaScript in HTML - Online JavaScript lessons for...

 AnvSoft Flash Slide Show Maker - display random picture from folder with flash
flash.dvd photo slideshow > myspace slideshow free

to enter the Flash Slideshow Website directly by selecting the...
If you use the Flash Slideshow for the first time or you want to...

 Cool Display Pictures, Avatars, Icons - avatarsvault

 Random Image Displayer
bezveze > skripte/rid

 Xaraya :: display random picture
xaraya > index.php/xarbb/topic/2658

Posted: Aug 17, 2006 05:03:10 PM...
Posted: Sep 4, 2006 08:06:07 AM...

 Revived Wire Media, formerly Big Lick Media - Scripts - php Random Image Displayer - web design, web development, hosting and corporate identity - Roa...
biglickmedia > misc/php/random image.php

 Random Image Display on Page - Dynamic Drive Forums
dynamicdrive > forums/showthread.php?p=65373

 Random Image Display script 0.5 Free Scripts Download - Random Image Display is a simple PHP script to display a random
rocketdownload > scripts/program/random image

Contacts Importer Address Grabber Invite Friends Import Gmail Hotmail...
Website Traffic Booster Sending Fake Hits Gets Higher...

 Display a random image, with JavaScript
mjrweb > latest/view

 Free random image display download - random image display script - Top 4 Download
scripts.top4download > free random image

Free random image display download - random image display script -...
This plugin requires that the Gengo plugin is installed as...
It can work as a template, inside a widget or with...

 PHP random image rotation script for your website
marcofolio > webdesign/php random image rotation

What am i doing wrong? Heres the HTML: Untitled...
Excellent script man! But somehow it doesn't work in IE, just shows a...
I would do anything for some info on hose to do as mentioned a...


Guest name   (option)     Register
Please sum : 2808 + one  

عرضت صورة عشوائيّة    (ar)
Дисплей на случаен принцип карти    (bg)
Zobrazuje náhodně obraz    (cs)
Udviser en tilfældig billede    (da)
Zeigen Sie eine gelegentliche Abbildung an    (de)
Επιδείξτε μια τυχαία εικόνα    (el)
Exhiba un cuadro al azar    (es)
نمايش‌ يك تصوير تصادفي‌    (fa)
Näyttö pistokokein kuva    (fi)
Montrez une image aléatoire    (fr)
Nuna da mahaɗin kafaɗa hoto    (ha)
תצוגה א אקראי התמונה    (he)
तस्वीर का यादृच्छिक प्    (hi)
Megjelenítés egy véletlenszerűen kiválasztott kép    (hu)
Visualizzi un'immagine casuale    (it)
任意映像を表示しなさい    (ja)
무작위 그림을 표시하십시오    (ko)
Toon een willekeurig beeld    (nl)
Skjerm en tilfeldig billede    (no)
Wyświetlacz losowo wybranej obrazu    (pl)
Indique um retrato aleatório    (pt)
Afisajul o intamplare tablou    (ro)
Покажите случайно изображение    (ru)
Ekran JEDAN slujan sliku    (sr)
Visa ett slumpmässigt föreställer    (sv)
จะแสดง ภาพ แบบสุ่ม    (th)
Bir Açığa gelişigüzel resim    (tr)
ایک بےقصد تصویر اویزاں    (ur)
显示一张任意图片    (zh)

Trackbacks : If you talked about this article in your blog or website, you may instantly get a backlink 
There's no trackback at the moment.