View Full Version : PHP Random Image Script
<?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.
leowyatt
25-02-2010, 22:48
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
<?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;
}
?>
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, 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 :/
leowyatt
26-02-2010, 17:47
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
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.
leowyatt
26-02-2010, 18:57
can I ask what this is for?
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).
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.
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. :)
well I'm setting it up for an 'unskilled' user so making things as simple as possible is best. The folder the images are taken from are all just photos of our team playing which makes them applicable to the majority of things posted to the site. I thought that was the best way to make things simple.
Presumably you're using a form to accept the articles, so sticking an image upload option on the end of that wouldn't add much complexity (for the user - it might for you).
If your team won something, you'd probably want to show a photo of them winning it, not something random. :)
PS - If you're going for the option of the user never wanting to take a photo then fair enough. Sounds rather limiting though.
Well I'm not creating the cms - as I say, I'm no programmer, just a designer with a little coding knowledge!
Currently not chosen the system to deploy but it's between MODx and Joomla - not too sure how either will handle images in this sense yet
vBulletin® v3.7.4, Copyright ©2000-2025, Jelsoft Enterprises Ltd.