I did that very thing in one of my scripts here is the code:
$dir = dir("./images/headers/");
while($file = $dir->read()) {
if (($file != ".") && ($file != "..")) {
$headers[] = $file;
}
}
$dir->close();
srand((double)microtime()*1000000);
$randval = rand (0,(count($headers)-1));
printf('<img src="images/headers/%s">', $headers[$randval]);