On Thu, 03 Jan 2008 01:44:55 +0000, Malcolm N_ <malcom (AT) mgnixon (DOT) org.uk>
wrote:
Quote:
So I have got a PHP random selector of an array of about 30 images
that display - and refresh if one hits the F5 key or refresh. |
Here's my humble contribution to the topic. Note that it will read a
specified directory and display a random image from that directory, so
if you want to add an image, all you need do is upload it. However, it
may not work on a Windows server running IIS because it relies on
$_SERVER['DOCUMENT_ROOT']. Other than that, just change the $imgdir line
to point to the directory you want to read. It defaults to read all
..jpg, .gif, and .png files. If you want to change that, the line below
the $imgdir line sets the types.
<?php
// root relative path where the images are stored
$imgdir = '/images/demo';
$types = array('.jpg','.gif','.png');
$imgpath = $_SERVER['DOCUMENT_ROOT'].$imgdir;
if($dp = @opendir($imgpath)){
$files = array();
$exp = '\\'.join('$|\\',$types).'$';
while (($file = readdir($dp)) !== false){
if (preg_match("/$exp/",$file))
$files[] = $file;
}
srand ((float) microtime() * 10000000);
$img = $files[array_rand($files)];
$img_sz = getimagesize("$imgpath/$img");
print "<img src=\"$imgdir/$img\" $img_sz[3]>\n";
}
?>
Gary