25-02-2010, 22:29 | #1 |
Absinthe
Join Date: Jan 2007
Location: Chester
Posts: 2,345
|
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]); ?> 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. |
25-02-2010, 22:48 | #2 |
Chef extraordinaire
Join Date: Jul 2006
Location: Infinite Loop
Posts: 11,143
|
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. |
25-02-2010, 22:54 | #3 |
Screaming Orgasm
Join Date: Jul 2006
Location: Newbury
Posts: 15,194
|
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. ) |
26-02-2010, 17:30 | #4 |
Absinthe
Join Date: Jan 2007
Location: Chester
Posts: 2,345
|
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 :/ |
26-02-2010, 17:47 | #5 | |
Chef extraordinaire
Join Date: Jul 2006
Location: Infinite Loop
Posts: 11,143
|
Quote:
to use the url you'll need to use the $_GET array
__________________
"Dr Sheldon Cooper FTW!" |
|
26-02-2010, 18:49 | #6 |
Absinthe
Join Date: Jan 2007
Location: Chester
Posts: 2,345
|
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. |
26-02-2010, 18:57 | #7 |
Chef extraordinaire
Join Date: Jul 2006
Location: Infinite Loop
Posts: 11,143
|
can I ask what this is for?
__________________
"Dr Sheldon Cooper FTW!" |
26-02-2010, 20:19 | #8 |
Screaming Orgasm
Join Date: Jul 2006
Location: Newbury
Posts: 15,194
|
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). |
26-02-2010, 20:59 | #9 |
Absinthe
Join Date: Jan 2007
Location: Chester
Posts: 2,345
|
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. |
26-02-2010, 21:11 | #10 |
Screaming Orgasm
Join Date: Jul 2006
Location: Newbury
Posts: 15,194
|
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.
|
Thread Tools | |
Display Modes | |
|
|