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.
|
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!
Also can the script search more then one directory? Thanks in advance
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.
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.
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.