<?php
if ($dir = opendir("."))
{
$list = buildimagearray($dir);
displayrandomimage($list);
}
// This function reads all the files in the current directory and adds all image files to the array $list[]
function buildimagearray($dir)
{
while (false !== ($file = readdir($dir)))
{
if (!is_dir($file) && getimagesize($file))
{
$list[] = $file;
}
}
return $list;
}
// This function selects a random image, determines the mime type, opens the file for reading,
// and then outputs the image
function displayrandomimage($list)
{
srand ((double) microtime() * 10000000);
$sig = array_rand ($list);
$size = getimagesize ($list[$sig]);
$fp = fopen($list[$sig], "rb");
if ($size && $fp)
{
header("Content-type: {$size['mime']}");
fpassthru($fp);
exit;
}
}
?>
