Jump to content

Getting random image, except those with a certain string


SamF

Recommended Posts

Hi,

 

I'm a PHP newbie trying to edit a PHP script that someone wrote for my website.

 

The php file below returns a random image from the directory it is in. While it works fine, I have two kinds of images in my directory -- full-size and thumbnails. My full-size images are called xxx.jpg. My thumbnails are calls xxx.thumbnail.jpg.

 

How can I change the php below so that it does NOT return any files with the string "thumbnail" in it?

 

Thanks a lot!

 

<?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 = '';

// 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!
?>

Link to comment
Share on other sites

Untested:

 

<?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 = '';

// 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
$loc = $folder.$files[$rand];

if(str_pos($loc, 'thumbnail') === false)
{
     header('Location: ' . $loc); // Voila!
}
else
{
     header('Location: ' . $_SERVER['PHP_SELF']); //Redirect to self and start over
}
?>

Link to comment
Share on other sites

Wait, I lied. It doesn't work perfectly.

 

If I navigate to the php file itself, the image changes randomly, as it should.

 

However, the image is embedded in my page as so:

<img src="http://www.jamesfentress.com/wp-content/uploads/random.php" alt="" width="48%" />

 

Now, originally, everytime I reloaded the page it showed me a new picture. Now however I can reload the page all I like and the picture won't change. Can anyone work out why?

 

(The website is www.jamesfentress.com. The image on the top right is the original version of the php file. The image below it is the new version.)

Link to comment
Share on other sites

Ok, so I tried adding both

header("Pragma: no-cache");

and

header("Cache-control: no-cache");

To the head of the php file above (not knowing the difference), but it made no difference -- I still only get the one image. Do I need to have it at the top of my home page? If so, that'll mess up cacheing of everything, right? (as, in general, I want cacheing).

 

Why does the original version of the php file not have this problem?

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.