PHP random image script

PLEASE NOTE: I cannot provide free installation support for this free script! Please contact me ONLY about possible bug reports, suggestions or comments! Thank you!



Copyright 2009 Klemen Stirn. All Rights Reserved.

This script may be used and modified free of charge by anyone AS LONG AS COPYRIGHT NOTICES AND ALL THE COMMENTS REMAIN INTACT. By using this code you agree to indemnify Klemen Stirn from any liability that might arise from it's use.

Using this script requires attribution. In other words: if you are using this script you are required to place a link to PHPJunkyard on your website. You will find some link suggestions here.

Selling the code for this program, in part or full, without prior written consent is expressly forbidden.

Obtain permission before redistributing this software over the Internet or in any other medium. In all cases copyright and header must remain intact. This Copyright is in full effect in any country that has International Trade Agreements with the United States of America or with the European Union.


RandIm is a simple PHP random image rotator script. You can use it to display random images from a folder of your choice. RandIm supports file extension filtering so it can be used to randomly pick other file types too, not just images!


» Installation

Please take 5 minutes time and read installation instructions carefully and completely! This will ensure a proper and easy installation.

If you have problems/questions see the "Troubleshooting and HELP" section further down.

-> Quick install

  1. FTP to the public folder of your server (where the rest of your website is; usually called "public_html", "www" or "site") and create a folder where you will install RandIm (for example named "randim").
    Example: /public_html/randim
    Corresponding URL: http://www.yourdomain.com/randim

    TIP: If you don't know how to FTP files read my simple FTP tutorial

  2. Upload all RandIm files and folders to your server. PHP files should be transferred in ASCII mode, images in binary mode.

That's it. To test if RandIm works fine on your server open randim/test.php in your browser, for example:

Your browser should show a sample page with 4 random images displayed. Try refreshing the page to see images change.

Got an error message? A few solutions can be found below under HELP and Troubleshooting.

-> Using random image script

Now that the script is up and running it's time to choose the images that should be randomly displayed. You can do so by uploading all your images to your "randim/images" folder.

Alternatively you can set RandIm to use images from any other folder of your choice by changing the default settings (read more about it further down).

To display a random image on your website using the default settings simply use the URL of randim.php as image SRC (source) address, for example:

<img src="http://yourdomain.com/randim/randim.php" />

As simple as that. By default, RandIm will randomly choose from any .jpg, .gif or .png files located in the "randim/images" folder.

Important! Using this script requires attribution. In other words: if you are using this script you are required to place a link to PHPJunkyard on your website. This is all I require in exchange for this free script, please support my work by placing a link on your website. You will find some link suggestions here.

-> Modifying default settings

To modify default settings open randim.php in a plain text editor (like Notepad or Wordpad on Windows, DO NOT use Microsoft Word!) and scroll down to where it says SETTINGS. Available settings are:

    Variable Description/Setup
    $settings['img_folder'] Name of the folder where RandIm will choose images (files) from by default. You can set it to any folder on your server, use relative or absolute (root) path to the folder you wish.

    For example if your images are located in a folder called "photos" located outside the "randim" folder, you would set it to:
    $settings['img_folder'] = '../photos/';
    $settings['img_ext'] File types (extensions) RandIm will choose from. You can add new extensions by adding ,'.extension' to the list.


    $settings['img_ext'] = array('.jpg');
    Effect: RandIm will choose from JPG files only

    $settings['img_ext'] = array('.jpg','.gif','.png','.tga','.bmp');
    Effect: RandIm will choose from JPG, GIF, PNG, TGA and BMP file types

    $settings['img_ext'] = array('.doc', '.pdf');
    Effect: RandIm will choose DOC and PDF files

    $settings['display_type'] If set to 1 RandIm will redirect to the chosen file. This is useful when you use RandIm from HTML pages as shown before (<img src="randim.php" />). It can also be used to create random file downloads if used in an anchor link, like this:

    <a href="http://yourdomain.com/randim/randim.php">Random file</a>

    If set to 0 RandIm will print path to the selected file. This mode should be used if you are using RandIm on a PHP page and want to use RandIm as a PHP include, for example:

    <img src="<?php include 'randim.php'; ?>">

    $settings['allow_otf'] If set to 1 RandIm will allow Changing settings on-the-fly (see below). Set to 0 to only allow hard-coded settings (as set in the randim.php file).

    For security purposes if $settings['allow_otf'] is set to 1 RandIm will only work with local folders and not remote ones (so people can't use your script to for example display images from their folder).

Save changes and upload the edited randim.php file to your server.

-> Changing settings on-the-fly

You can also modify RandIm settings on-the-fly simply by passing proper values to the script using query string (when using RandIm as URL) or variables (when using RandIm as a PHP include).

For changing settings on-the-fly to work variable $settings['allow_otf'] must be set to 1 (see above)!

Operating mode ($settings['display_type']) can be changed using $_GET['type'] variable and images folder using $_GET['folder'] variable. This might sound a bit confusing, but here are a few examples:

If you want to display one random image from folder "images" and one from folder "images2" you could use this code:

<img src="http://yourdomain.com/randim/randim.php?folder=images" />
<img src="http://yourdomain.com/randim/randim.php?folder=images2" />

The same thing if you are using RandIm as a PHP include:

<img src="<?php $_GET['folder'] = 'images'; include 'randim.php'; ?>">
<img src="<?php $_GET['folder'] = 'images2'; include 'randim.php'; ?>">

If your $settings['display_type'] is set to 1 and default folder set to "images" but you would like to also use randim.php as include and choose a file from folder "../photos" try this:

<img src="<?php $_GET['type'] = 0; $_GET['folder'] = '../photos'; include 'randim.php'; ?>">

And if your display_type is set to 0 by default (using RandIm as a PHP include) you can override the default settings and use randim.php as an URL too:

<img src="http://yourdomain.com/randim/randim.php?type=1&folder=../photos" />

The same code is used in the test.php file, have a look if interested.

A lot of time and effort went into developing RandIm and other PHPJunkyard scripts. That's why this script requires attribution. In other words: if you are using this script you are required to place a link to PHPJunkyard on your website. This is all I require in exchange for this free script, please support my work by placing a link on your website. It can be as simple as a short text link.

-> Don't want to place a link?

If you want to use RandIm but would prefer (for any reason) not to place a link to PHPJunkyard on your website you can attribute by sending a small donation. Any amount. Donations help keep PHPJunkyard website and scripts alive.

-> Link suggestions

1. Simple text link
Random image

2 "Powered by" simple text link
Powered by Random image

3. Text link with description
Random image - get this and other free PHP scripts from PHPJunkyard.

TIP: You can use one of the PHPJunkyard banners that come with RandIm (check inside images folder) for the link or get more link suggestions on the PHPJunkyard link to us page!

» HELP and Troubleshooting

1. What is CHMOD and FTP?

I have prepared a simple FTP and CHMOD tutorial which will help you FTP files to your server and set correct CHMOD settings.

2. Images won't show! I'm getting a NO IMAGES FOUND message?

Make sure that:

  1. the path to your images folder is correct (check $settings['img_folder'])
  2. there are images in your images folder
  3. image types are allowed (check $settings['img_ext'])
  4. you are using correct display_type (1 if using RandIm as URL, 0 if as PHP include. Check $settings['display_type'])

3. The on-the-fly changes have no effect

Make sure you have $settings['allow_otf'] set to 1 and are passing correct values to the script. If you pass invalid values the default settings will be used


Since these scripts are free no support is guaranteed. If you can't get the script to work please go through this readme file again carefully and repeat the installation step-by-step (also delete old files and folders from the server). Also please feel free to post any questions or problems you might have in PHPJunkyard forum!

» Stay updated!

Join my FREE newsletter and you will be notified about new scripts, new versions of the existing scripts and other important news from PHPJunkYard.
Click here for more info

» Please rate this script

If you like this script please rate it or even write a review at:

Rate this Script @ Hot Scripts

Rate this Script @ The PHP Resource Index

Best regards,

Klemen Stirn
PHP JunkYard