This is a VERY round-about way to do it, but it seems to work...
//Specify the path to the files
$directoryPath="temp/";
if ($dir = @opendir("$directoryPath")) {
//Make sure it is not a directory, just a file
while (($file = readdir($dir)) !== false) {
//Add the file name to the array
if(is_file("$directoryPath$file")){
$fileArray[]="$file";
}
}
closedir($dir);
}
//Pick a random file name out of the array
$fileName=$fileArray[rand(0,count($fileArray)-1)];
//Get the file extension
$ext=explode(".",$fileName);
$ext=$ext[1];
//If it is an image, including the file will display gobbledy-gook
//So, we need to print the image.
if($ext=="gif" or $ext=="jpg"){
print("<img src=\"$directoryPath$fileName\">");
//if it is a file, just include it.
}else{
include("$directoryPath$fileName");
}