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?

Link to comment
Share on other sites

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"; }
?>

Ā 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.