Zwixy
contacthomepaw3gallerypaw4tutorialspaw5Homepaw1tospaw2
Random image from folder with PHP

On the Zwixy main page we have a random image displayed every time you refresh the page. I almost forgot to make a little page for it so here i go. You can use this to display random images from a folder on your shared hosting or your own server.

You can adjust it more to make the images link to the display page or just the picture. You could also use it to display random banners if you plan on putting custom advertising on your pages.

The code itself is very simple and easy to use:

<?
$imglist='';
$img_folder = "folder"; #Don't forget to put / in the end#

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

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

//reads all the files from the folder and checks if they are images and then ads them to a list
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>';
?>

 

Feel free to play around with it (i did so too) and let us know what you did by leaving a comment down here. If you have more ideas to add to this, let us know and just put the code in the comment if you wish to do so. I got the idea (and most of the code) from another website from which i can't remember the name so credit to whoever came up with it first i guess.

 

Comment Script
Post this page to: del.icio.us Yahoo! MyWeb Digg reddit Furl Blinklist Spurl

Comments

I tried this code and had no problem with it ... Kobyn seems strange ;)
#9 - rami - 11/29/2009 - 16:26
Hey, Beegee

I sent you a mail to the address you gave with the comment :) In case you don't check it that often or gave a fake one ;)

If you didn't get it then send me a mail at info@zwixy.com

Take care!
#8 - Zwixy - 09/19/2009 - 10:52
HI great script, but I have two quesitons for you. first I am looking for exactly what you do on your homepage, show a thumbnail or smaller version that links to the full sized image. Can U ask how you did this. I got the script itself working no problem but only shows the full size image.

Also can the script search more then one directory? Thanks in advance
#7 - beegee - 09/19/2009 - 09:05
Thank you Zwixy.
#6 - Kobyn - 07/13/2009 - 10:47
Are you sure you're using the right folder and the right path? Don't forget / in the end either.

That's all i can come up with so far. I'm using that exact same piece of code for the homepage here so i know it works.
#5 - zwixy - 07/13/2009 - 08:30
displaying
Hi zw,

Thanks for your promptly response. I got NO errors. It just doesn's show anything. I just copy and paste your code and I change the dir.
#4 - Kobyn - 07/13/2009 - 07:58
Doesn't work
I am playing aroung with your code but it doesn't work at all.
#3 - Kobyn - 07/13/2009 - 07:55
Heya, Kobyn.

What exactly are you doing? What error is showing up? Can you email me the piece of code you've done? You can find my contact details on the contact page.
#2 - zwixy - 07/13/2009 - 07:50
Doesn't work
I am playing aroung with your code but it doesn't work at all.
#1 - Kobyn - 07/13/2009 - 07:47
Better?
#0 - Tim - 02/26/2009 - 02:59
Name
E-mail (Will not appear online)
Homepage
Title
Comment
This comment form is powered by GentleSource Comment Script. It can be included in PHP or HTML files and allows visitors to leave comments on the website.
copyright zwixy.com 2009 - home | faq | terms of service | gallery | privacy | tutorials | contact us