Populate with directory of images


I am a non programmer with absolutely no programming experience, except I know it is a variable when I see $ sign. That’s about it. So, most I can do is cut and paste from examples. So far none of the examples that I have found are working well for me. Been messing with it for 2 days.

Apologize as this has probably been asked a thousand times. I have this following simplest code. This is from the index.php of Joomla. To set a background image for the web page to full size using backstretch script.

function random_background() {

 $img_array[1] = "bagan.extrawide.background.jpg";
 $img_array[2] = "inle.background.jpg";
 $img_array[3] = "shwedagon.stairs.background.jpg";

 srand ((double) microtime() * 1000000);
 $random = rand(1, count($img_array));
 return $img_array[$random];


// To set the background for the session. Not on every page visit.
$bg_image = $_SESSION['background'];
$bg_image = random_background();
$_SESSION['background'] = $bg_image;

Then somewhere further down, I have this Javascript to place the background. That’s all.

<script type="text/javascript" src="/js/jquery.js"></script>
<script src="/js/jquery.backstretch.js" type="text/javascript"></script>

<script type="text/javascript">

$.backstretch("/images/<?php echo $bg_image ?>");


All I am wanting to do is instead of having to add $img_array[1-2-3-4-5] every time I want to add an image. What would be the simplest code to pull all the images from a given directory. I am using PHP4 server. Not even sure if what I have is good coding or not, but it at least works ok. I know I have to do it with the opendir thing. The following code I have found got it very close to working.

function RandomFile($folder='', $extensions='.*'){
    // fix path:
    $folder = trim($folder);
    $folder = ($folder == '') ? './' : $folder;
    // check folder:
    if (!is_dir($folder)){ die('invalid folder given!'); }
    // create files array
    $files = array();
    // open directory
    if ($dir = @opendir($folder)){
        // go trough all files:
        while($file = readdir($dir)){
            if (!preg_match('/^\\.+$/', $file) and 
                preg_match('/\\.('.$extensions.')$/', $file)){
                // feed the array:
                $files[] = $file;                
        // close directory
    else {
        die('Could not open the folder "'.$folder.'"');
    if (count($files) == 0){
        die('No files where found :-(');
    // seed random function:
    // get an random index:
    $rand = mt_rand(0, count($files)-1);
    // check again:
    if (!isset($files[$rand])){
        die('Array index was not found! very strange!');
    // return the random file:
    return $folder . $files[$rand];

And got lost when trying to take out the trim folder because I’m using rewrite and wouldn’t work well since I need the directory name with /images/ not images/. Appreciate the help and guidance.

  1. Upgrade to PHP5.
  2. [FPHP]scandir[/FPHP] or [FPHP]glob[/FPHP]