Here is the code I use to generate a random image from any number of images in a directory.
function Generate_Random_Number($max) {
// srand is needed to seed the random number generator
srand((double)microtime()*1000000);
$random = rand(0,$max);
return $random;
}
function Show_Random_Image($directory) {
if (is_dir($directory)) {
$handle = opendir($directory);
while (($file = readdir($handle)) !== false) {
if ($file != "." && $file != "..") {
// place every non "." ".." image into the array $max
$max[] = $file;
}
}
closedir($handle);
}
$random = Generate_Random_Number(sizeof($max) - 1);
$image = $directory.$max[$random];
$dimensions = GetImageSize($image);
$code = "<img src=\"$image\" $dimesions[3] border=\"0\" alt=\"\">";
//debug $code = "$random - $image - $dimensions[3]";
return $code;
}
in my page:
<?php
echo Show_Random_Image('/images/something/');
?>
The first function just generates a random # and can be used as is. The second function reads all of the files in a directory (the directory name is passed to the second function when I call it on my page) and then returns the code to show a random one using the code in the first function.
I always use a database instead of a text file, but I am sure you can use a combination of fopen() and fgets() (see http://www.php.net/manual/en/ref.filesystem.php) to modify my second function to read a random line from a file.
Good luck!
Erik