Random Image Script

Released on May 10, 2003
Last Updated: May 10, 2003 3:44 AM
Version: 1.1


This is your standard random image script that takes a slightly different approach than most, namely using standard HTTP headers instead of reading the file through the script.


For the simplest possible installation, just drop this code in a directory with the images you want to rotate, and call it like you would a normal image, for example:

<img src="/dropbox/2003/rotate/rotate.php" alt="A Random Image" />

A Random Image


A Rotator Apart

By Matt Mullenweg >
Inspired by Dan Benjamin >
Latest version always at:
// Make this the relative path to the images, like "../img" or "random/images/".
// If the images are in the same directory, leave it blank.
$folder = '';

// Space seperated list of extensions, you probably won't have to change this.
$exts = 'jpg jpeg png gif';

$files = array(); $i = -1; // Initialize some variables
if ('' == $folder) $folder = './';

$handle = opendir($folder);
$exts = explode(' ', $exts);
while (false !== ($file = readdir($handle))) {
foreach($exts as $ext) { // for each extension check the extension
if (preg_match('/\.'.$ext.'$/i', $file, $test)) { // faster than ereg, case insensitive
$files[] = $file; // it's good
closedir($handle); // We're not using it anymore
mt_srand((double)microtime()*1000000); // seed for PHP < 4.2
$rand = mt_rand(0, $i); // $i was incremented as we went along

header('Location: '.$folder.$files[$rand]); // Voila!


For additional information about the thinking behind this script, see A Better Image Rotator.

42 replies on “Random Image Script”

[…] Yes, that’s right. I showed that big blue box who’s boss before any of you had the chance to even realize what on earth I’m talking about. I added a new cascading style sheet to this puppy, paired with a cool little random image script, courtesy of PhotoMatt. All images currently in the random shuffle are courtesy of Mando. Hopefully I’ll continue to add more images to the rotation. I’ll most likely add some of my own once I have access to the high-res versions, which happen to be back home on my computer… I think… […]

[…] I spent a few hours installing themes until I found one that I could customize to my liking. I choose the blue horizon theme.  One of the things I wanted to incorporate was the rotating banner that Darcy Norman so elegantly uses.  I had installed it on a previous install I did for a client but wanted to include my own photos.  So everytime you come you’ll see a different header.  All photos are taken by me and I include the details on the bottom left corner. I’m not much of a photographer but love taking photos and playing around with them. […]

Nice code, thanks.

I suggest you put the next two lines just before the last line:
header(“Cache-Control: no-cache, must-revalidate”); // HTTP/1.1
header(“Expires: Mon, 26 Jul 1997 05:00:00 GMT”);

This will prevent the browser and/or the cache-server to cache the images. Otherwise the randomness is pretty poor.

It works, but as Ondra said, the randomness is not necessarily all that.

However, I inserted those two lines of code, and it didn’t fix it. In fact, my page reverted to the default Kubrick header image when I did that.

When I took those lines out, it went back to one of my images that’s in the same directory as the script. But it’s not really rotating at all.

Guys, some browser hold on to the cache. You may need to do a force reload. (Ctrl + F5)

Your visitors aren’t going to be sitting there reloading your site, they’re just going to see something different each time they come.

Yeah, I just noticed that my problem seems to be a Firefox issue. (I’m running Firefox under Mac OSX 10.4.6. Firefox has other “issues” as well.)

It works just fine in Safari. And, as Matt says, a header image is just a header image. They aren’t going to be sitting there clicking all day, like we do!


i searched very long for such a random script like this. It works perfectly! Thank You

[…] I’ve just installed a new rotating header for the site, using the random image script from Photo Matt, and the images from Every time you refresh this site, a new header image will appear. I think that’s a prett cool way to keelp the site fresh, with, relatively, minimal effort. […]

Club webpage was boring and static… now with a random pic loading, it gives it more pazazz and gives it that “professional” touch that looks oh-so nice… Thanks Matt, your code did the trick!

Wow! Thank you. That script rocks. I took a class in C a long time ago, and trying to get back into the swing of things with this webpage. I have to admit seeing that you closed the open file is clean up that’s uncommon to see with people scripting stuff. Good clean code!

I figured out how to get it to resolve on FireFox. You can’t let it resolve the path for you, you have to hard code the full file path with the specific directory to the rotate.php in the calling code’s img src.

Thanks for the script! I have gone through multiple ones with multiple variables needing multiple configuration changes. Someone should delete every other random image script from the Internet.

Thanks for the script, it works great on my blog. I eventually figured out that the forced-reload (CTRL-F5) was necessary to test the randomness, and I put the image-fetching script inside of an un-ordered list tag to scoot it a bit to the right so it doesn’t crowd the left edge of my navigation column. If only I could associate a link with each image, that would enable site visitors to click on an image that would take them to a specific section or page of my website. Would that even be possible, do you think?

Thank you so much Matt!! I was looking for ages to find a decent script that would work with my current PHP scripts and this was it!!


Matt, I keep finding myself back at your site lately…
Was about to build something like this but you beat me to it, excellent.
I chucked it into Drupal’s Kubrick theme port and it works great on