SamF Posted February 25, 2007 Share Posted February 25, 2007 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! ?> Quote Link to comment Share on other sites More sharing options...
superuser2 Posted February 25, 2007 Share Posted February 25, 2007 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 } ?> Quote Link to comment Share on other sites More sharing options...
SamF Posted February 25, 2007 Author Share Posted February 25, 2007 Hmmm, I get this error in Firefox: Fatal error: Call to undefined function: str_pos() in /[my path]/random.php on line 26 Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 25, 2007 Share Posted February 25, 2007 it's strpos() Quote Link to comment Share on other sites More sharing options...
SamF Posted February 25, 2007 Author Share Posted February 25, 2007 That works great, thanks! Quote Link to comment Share on other sites More sharing options...
SamF Posted February 25, 2007 Author Share Posted February 25, 2007 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.) Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 25, 2007 Share Posted February 25, 2007 Because your browser caches it. You need to add no-cache headers to the page. Search the forum, they've been posted many times. Quote Link to comment Share on other sites More sharing options...
SamF Posted February 25, 2007 Author Share Posted February 25, 2007 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? Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 25, 2007 Share Posted February 25, 2007 It needs to be on the page that produces the image, and the one the image is displayed on. Quote Link to comment Share on other sites More sharing options...
shoz Posted February 25, 2007 Share Posted February 25, 2007 If the original script works as you want it to, you can replace the relevant line with the following to ignore thumbnails. if (preg_match('/(?<!thumbnail)\.'.$ext.'$/i', $file, $test)) { // faster than ereg, case insensitive Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.