This is really quickly written, so it's not the best code, but here's an example of how to make it work all in one file. You can name this whatever, it just needs to be parsed as a PHP file. index.php might be good so it'll load by default if you try to get to the images directory (if it does in fact sit in said directory). Heavily commented to try and make it easier for you. If something needs clarification, by all means.
PHP Code:
<?php
# Where are images? Leave blank to default to working directory.
# It MUST end with a slash if you set something or it will not work!
$img_dir = '';
# What image extensions are allowable?
# Use array( 'value', 'value' ); syntax.
$img_exts = array('jpg', 'jpeg', 'png');
# No more editing is required.
$images = array();
# Blank directory? Assume current directory, then.
if ( $img_dir == '' ) {
$img_dir = './';
}
# Set dirhandle. If failed, error out.
if(!$dirh = opendir($img_dir)){
die("Could not open directory: {$img_dir}");
}
while ( false !== ($file = readdir($dirh)) ) {
# Grab the extension (everything after the last dot).
$fext = substr($file, strrpos($file, '.') + 1);
# Don't include the ., .., or any folder as results.
if ( $file != '.' and $file != '..' and !is_dir($file)) {
# See if this file's extension is in the img_exts array.
# This array contains allowed extensions.
if ( in_array ( $fext, $img_exts ) ) {
$files[] = $file;
}
}
}
closedir($dirh);
# No need to seed since PHP 4.2.0. This takes any image from
# 0 to the highest index in the $files array, then prepends the
# image directory to the name of the file.
$random_image = rand(0, count($files));
$random_image = $img_dir . $files[$random_image];
# This is a heredoc--consider it a multilined print(), simply.
print <<<OUTPUT
<html>
<head>
<title>Random Image</title>
</head>
<body>
<p>Click the image below to pick another random one.</p>
<center>
<a href="{$_SERVER['PHP_SELF']}"><img src="{$random_image}"></a>
OUTPUT;
?>
This is very bad HTML (not even completed), but I wasn't going for that, since it'll show more or less what to do.
As a note, you see the { } braces wrapping $_SERVER['PHP_SELF'] in the last bit? Those make it so PHP won't error out on the print, since it doesn't like quoted array indices in prints for some reason. Wrapping the variable is the same as concatenating it outside of the quote, essentially.
Hopefully that is of help for you.
There's nowhere I can't reach.