Boat Drinks  

Go Back   Boat Drinks > General > Computer and Consoles

Reply
 
Thread Tools Display Modes
Old 25-02-2010, 22:29   #1
Joe 90
Absinthe
 
Joe 90's Avatar
 
Join Date: Jan 2007
Location: Chester
Posts: 2,345
Default PHP Random Image Script

Code:
<?php
$fileList = array();
$folder = ".";
$handle = opendir($folder);
while (
false !== ( $file = readdir($handle) )
) {
if (
substr($file, -4) == ".gif" ||
substr($file, -4) == ".jpg"
) {
$fileList[count($fileList)] = $file;
}
}
closedir($handle);
$randNum = rand( 0, (sizeOf($fileList) -1) );
if (
substr($fileList[$randNum], -4) == ".gif"
) {
header ("Content-type: image/gif");
} elseif (
substr($fileList[$randNum], -4) == ".jpg"
) {
header ("Content-type: image/jpeg");
}
readfile($fileList[$randNum]);
?>
So, I've got this script running and it displays a random image okay, but it appears that cache can cause the same image to be displayed once, and then again and again for the remainder of the instances where the script is called.
http://www.shuwarriors.net

I'm no programmer so don't know enough about php - is there a way to limit the amount of times 1 image can be posted - or some other kind of fix?
thanks.
__________________
360 Blog | Join GiffGaff | Twitter
Joe 90 is offline   Reply With Quote
Old 25-02-2010, 22:48   #2
leowyatt
Chef extraordinaire
 
leowyatt's Avatar
 
Join Date: Jul 2006
Location: Infinite Loop
Posts: 11,143
Default

you could always write the filename to an array then just stick the an if around it:

if(!in_array($filename, $arrayOfFileNames){
readfile($fileList[$randNum]);
}

I'd also swap your if else extension statements for a switch case
Code:
<?php
$fileList = array();
$viewedFiles = array();
$folder = ".";
$handle = opendir($folder);
while (false !== ( $file = readdir($handle) )) {
	if (substr($file, -4) == ".gif" || substr($file, -4) == ".jpg") {
		$fileList[] = $file;
                
	}
}
closedir($handle);
$randNum = rand( 0, count($fileList)-1 );

  $ext = substr($fileList[$randNum], -3);
  switch(strtoupper($ext)){
        case "JPG":
	   header ("Content-type: image/jpeg");
        break;
        case "GIF":
           header ("Content-type: image/gif");
        break;
    }

  if(!in_array($file[$randNum], $viewedFiles){
     readfile($file[$randNum]);
     $viewedFiles[$file] = $file;
  }
 

?>
__________________
"Dr Sheldon Cooper FTW!"

Last edited by leowyatt; 25-02-2010 at 23:19.
leowyatt is offline   Reply With Quote
Old 25-02-2010, 22:54   #3
Mark
Screaming Orgasm
 
Join Date: Jul 2006
Location: Newbury
Posts: 15,194
Default

Change the URLs to look like this:

http://www.shuwarriors.net/images/main_photos/random.php?id=1234

It doesn't matter what the numbers are - as long as each one is different.

That might do the trick (do three to see if it works before doing the rest. )
Mark is offline   Reply With Quote
Old 26-02-2010, 17:30   #4
Joe 90
Absinthe
 
Joe 90's Avatar
 
Join Date: Jan 2007
Location: Chester
Posts: 2,345
Default

Mark, how would you do that? i've only ever used URLs like that with database content. :/

and cheers leo, although i got an unexpected '{' in line;
if(!in_array($file[$randNum], $viewedFiles){

i can't see why that is unexpected :/
__________________
360 Blog | Join GiffGaff | Twitter
Joe 90 is offline   Reply With Quote
Old 26-02-2010, 17:47   #5
leowyatt
Chef extraordinaire
 
leowyatt's Avatar
 
Join Date: Jul 2006
Location: Infinite Loop
Posts: 11,143
Default

Quote:
Originally Posted by Joe 90 View Post
Mark, how would you do that? i've only ever used URLs like that with database content. :/

and cheers leo, although i got an unexpected '{' in line;
if(!in_array($file[$randNum], $viewedFiles){

i can't see why that is unexpected :/
Joe it's because i didn't close the if off properly

to use the url you'll need to use the $_GET array
__________________
"Dr Sheldon Cooper FTW!"
leowyatt is offline   Reply With Quote
Old 26-02-2010, 18:49   #6
Joe 90
Absinthe
 
Joe 90's Avatar
 
Join Date: Jan 2007
Location: Chester
Posts: 2,345
Default

you mean like mark said - i need to assign an ID to each instance of the script?

if so - is there a method to automatically assign a random number to that? because eventually I want to be able to put this into the template so that the user only has to add new content and not deal with the images.
__________________
360 Blog | Join GiffGaff | Twitter
Joe 90 is offline   Reply With Quote
Old 26-02-2010, 18:57   #7
leowyatt
Chef extraordinaire
 
leowyatt's Avatar
 
Join Date: Jul 2006
Location: Infinite Loop
Posts: 11,143
Default

can I ask what this is for?
__________________
"Dr Sheldon Cooper FTW!"
leowyatt is offline   Reply With Quote
Old 26-02-2010, 20:19   #8
Mark
Screaming Orgasm
 
Join Date: Jul 2006
Location: Newbury
Posts: 15,194
Default

The problem you've got is that the browser, logically, sees all those identical URLs and only makes one request. Logical really - think what would happen if it didn't - every single 'Quote' button in this thread would get downloaded separately. That'd be horrendous - cache or not.

Now, how you fix it depends on how the images are placed on the webpage. If it's just squeaky clean HTML, then just edit the URLs. If the images come from an article in a database, use the Article ID in the image URL somewhere (this only works for one image per article). If the images are placed using PHP, get the last modification time of the image and use that. If all else fails (and this must be a last resort as it has undesirable consequences), then use mt_rand() to generate an ID (it'll be unique 'enough').

Bear in mind though that whatever you do, it'll ramp up your bandwidth usage (and slow down page loads).
Mark is offline   Reply With Quote
Old 26-02-2010, 20:59   #9
Joe 90
Absinthe
 
Joe 90's Avatar
 
Join Date: Jan 2007
Location: Chester
Posts: 2,345
Default

at the moment its for the main page of; www.shuwarriors.net

currently it's all static content - but for my dissertation I'm implementing a content management system and hopefully i'll be able to include this script in that so that each piece of content displayed by the system will have an image displayed with it taken from a folder of images on our server.
__________________
360 Blog | Join GiffGaff | Twitter
Joe 90 is offline   Reply With Quote
Old 26-02-2010, 21:11   #10
Mark
Screaming Orgasm
 
Join Date: Jul 2006
Location: Newbury
Posts: 15,194
Default

In the CMS case, wouldn't you want the images to be relevant to the article? In which case, a random image probably isn't what you want anyway.
Mark is offline   Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 18:04.


Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.