Jump to content

Load multiple images randomly from a directory


reptkeu

Recommended Posts

Hello,

I wanted to have a page on my website that displays lots of images randomly from a folder.

I found  this article and this this code which exactly does what I am looking for.

Problem is: the image folder has to be called "random" and has to be located at in the ROOT directory. (Oh, and one more problem: I am a total newbie in coding, just some html basic knowledge.)

I keep trying to modify the script to point toward the right location but it just won't work.

Can you guys help?

This should do exactly as you seek..

I was sitting bored one night and I had a spare domain that was sitting doing nothing.. So I decided to put a random image gen on it. Nothing special about this script.

 

http://www.uglygoods.com if you want to see it working, just refresh the page. a couple times..

 

<?php
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
        closedir($dir);    
    }
    else {
        die('Could not open the folder "'.$folder.'"');
    }

    if (count($files) == 0){
        die('No files where found :-(');
    }

    // seed random function:
    mt_srand((double)microtime()*1000000);

    // 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];
}
?>

<img src="<?php print RandomFile('img/','jpg|png|gif'); ?>" />
<img src="<?php print RandomFile('img/','jpg|png|gif'); ?>" />
<img src="<?php print RandomFile('img/','jpg|png|gif'); ?>" />

<?php
//This Bit only returns a count of files in a given directory
$dir = "img";
$file_count = -3;
// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
  if ($dh = opendir($dir)) {
    while (($file = readdir($dh)) !== false) {
      //echo "filename: $file : filetype: " . filetype($dir . $file) . "<br />";<br />
    $file_count++;
    }
    closedir($dh);
  }
  else { echo "x"; }
}
else { echo "o"; }
?>

 

I replaced

 

$random1 = RandomFile("random");
while (!$random2 || $random2 == $random1) {
$random2 = RandomFile("random");
}

 

with

 

$random1 = RandomFile("http://bertrandclerc.com/autres/random_images/random/");
while (!$random2 || $random2 == $random1) {
$random2 = RandomFile("random");
}

 

But it doesn't work. Any idea?

This should do exactly as you seek..

I was sitting bored one night and I had a spare domain that was sitting doing nothing.. So I decided to put a random image gen on it. Nothing special about this script.

 

http://www.uglygoods.com if you want to see it working, just refresh the page. a couple times..

 

Hi Monkeytooth. It works great!

I couldn't even get the one I mentionned at first to work. And I think even if I did, it would have been a nightmare (I want to display dozens of pictures and with this first code I would have had to copy/paste/modify the three lines everytime I want an iamge to display.

With yours, it's much easier and quicker (just copy/paste......)

Thanks a lot for your help.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.