View Full Version : Full Directory Image Pre-Loader (uses PHP aswell)

06-27-2011, 06:51 PM

I always found i wanted a pre-loader to load a whole directory of images (not really suitable for many images).

This was because if i added an image to a directory i didn't want to have to add that image to the pre-load script. This also removes the spelling errors that humans make in naming files.


v1.1 - Error fix... The phpImage and javaImage variables were incorect and have now been fixed by removing the codetype prefix and un-capitalizing the initial I.

v1.2 - Addition of terms of use and instructions for use.


Here it is:

<script type="text/javascript">


Full Directory Image Pre-Loader

Deisgned by Laurence Care
This code is free to use for any purpose though please leave this notice in any copies taken.

This script will only work if used in a file with the extension .php


// create counter
var i = 0;

// creat object
imageObj = new Image();

// build array of images
images = new Array();


// YOUR INPUT IS REQUIRED HERE... set the name of the folder (replace "<foldername>")
define ('FOLDER','<foldername>');

// build array of files in folder
$image = glob(FOLDER . "/" . "*.*");

// create counter
$i = 0;

// loop through directory
foreach ($image as $value)
// create array for javascript loop
echo "images[" . $i . "] = '../" . $value . "';";

// next number in array



// get array length
var x = images.length;

// loop through java array and load images.
for(i=0; i<=x; i++)


I found it useful.