Why are my random images not random?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
LPent
Forum Newbie
Posts: 5
Joined: Sun Oct 12, 2008 5:30 am

Why are my random images not random?

Post by LPent »

I found this piece of script:

Code: Select all

<?php
// Make this the relative path to the images, like "../img" or "random/images/".
// If the images are in the same directory, leave it blank.
$folder = 'img/random/';
 
// Space seperated list of extensions, you probably won't have to change this.
$exts = 'jpg jpeg png gif';
 
$files = array(); $i = -1; // Initialize some variables
if ('' == $folder) $folder = './';
 
$handle = opendir($folder);
$exts = explode(' ', $exts);
while (false !== ($file = readdir($handle))) {
foreach($exts as $ext) { // for each extension check the extension
if (preg_match('/\.'.$ext.'$/i', $file, $test)) { // faster than ereg, case insensitive
$files[] = $file; // it’s good
++$i;
}
}
}
closedir($handle); // We’re not using it anymore
mt_srand((double)microtime()*1000000); // seed for PHP < 4.2
$rand = mt_rand(0, $i); // $i was incremented as we went along
 
header('Location: '.$folder.$files[$rand]); // Voila!
?>
 
And I use this in my html page:

Code: Select all

<img src="rndimg.php" />
    <img src="rndimg.php" />
    <img src="rndimg.php" />
This produces three the same images. I want three different images.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Why are my random images not random?

Post by Eran »

That script does a lot of unnecassary things, especially the redirect at the end.
Try this, it's tested and it works:

Code: Select all

<?php
$files = glob('/path/to/files/{*.jpg,*.gif,*.png,*.jpeg}',GLOB_BRACE);
$file = $files[rand(0,count($files))];
header('Content-type: image/' . substr($file,strrpos($file,'.')+1));
echo file_get_contents($file);
?>
LPent
Forum Newbie
Posts: 5
Joined: Sun Oct 12, 2008 5:30 am

Re: Why are my random images not random?

Post by LPent »

That is a lot neater code indeed and yes, it does exactly the same thing. Including giving me 3x the same image LOL. I want 3 different images.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Why are my random images not random?

Post by Eran »

I tested it - it works.. perhaps you should check the folder and file associations you have there.

Also, remove the header() and file outputting, and dump the $files array. I think you'll immediately know why the scripts aren't working for you.
LPent
Forum Newbie
Posts: 5
Joined: Sun Oct 12, 2008 5:30 am

Re: Why are my random images not random?

Post by LPent »

I have feeling you don't understand?

The script works in that it loads a random image from the folder, but I want to load 3 random images from that folder. It does, but they are ALWAYS 3x the same image.

So let us say I have 1.jpg, 2.jpg ... 9.jpg The output will always be: 2.jpg 2.jpg 2.jpg and I want 2.jpg 7.jpg 4.jpg or something similar.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Why are my random images not random?

Post by Eran »

Oh... :P
I guess I misunderstood. Then the problem is that the script is probably cached for the duration of the page rendering. Add the following to the invocation of the script:

Code: Select all

 
<img src="rndimg.php?r=<?php echo rand(0,1000000); ?>" />
<img src="rndimg.php?r=<?php echo rand(0,1000000); ?>" />
<img src="rndimg.php?r=<?php echo rand(0,1000000); ?>" />
 
LPent
Forum Newbie
Posts: 5
Joined: Sun Oct 12, 2008 5:30 am

Re: Why are my random images not random?

Post by LPent »

oh wow, it works!! :P
Thank you very very much!
Post Reply