Random Image Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
uberpolak
Forum Contributor
Posts: 261
Joined: Thu Jan 02, 2003 10:37 am
Location: Next to the bar

Random Image Problem

Post by uberpolak »

I've created a random banner script, and am having a strange problem. I have a file called "banners.lst" which contains the names of the banner files. The script looks like this:

Code: Select all

<?php

//bnr.php

$banners = file("banners.lst");

header("Location: " . $banners[rand(0,sizeof($banners))]);

?>
I then call the script like this from an html file:

Code: Select all

&lt;IMG SRC="bnr.php" WIDTH="468" HEIGHT="60" ALT="random banner"&gt;
Weird thing happens, it works fine most of the time, but every once in a while it doesn't load an image, just gives the Internet Explorer corrupted/nonexistant image box (red x with alt text). I tested it with a very small amount of images (4), none of them are corrupt, as all of them loaded at some time. What I think might be happening is that a blank line is being appended to my banners.lst file, and the script is trying to load the blank line. Does anyone either a) know how to stop this from happening or b) see something other than that which would cause this not to work? Thanks for input.
User avatar
Kriek
Forum Contributor
Posts: 238
Joined: Wed May 29, 2002 3:46 am
Location: Florida
Contact:

Post by Kriek »

Put the files names in banners.lst and include() this PHP.

Code: Select all

<?php
$tmp=0;
// Get Image
$fp = fopen ("banners.lst","r");
while ($data = fgetcsv ($fp, 2000, "|")) {
 if ($data[0]) {
  $Img[$tmp] = trim($data[0]);
  $tmp++;
 }
}
fclose ($fp);

$count = $tmp-1;
srand((double) microtime() * 1000000);
$loopcount=0;
while(!$Img[$Pull] && $loopcount < 100) {
    $Pull = rand(0,$count);
    $loopcount++;
}
// Display Image  
if ($Img[$Pull]) {
    echo "<a href='whatever.php'><img src='$Img[$Pull]' border=0></a>";
} else {
    echo "<a href='whatever.php'>Link Name</a>";
}
?>
Post Reply