[thelist] php image rotation problem

Burhan Khalid thelist at meidomus.com
Tue Mar 15 04:52:33 CST 2005


Bruce Gilbert wrote:
> I am using the following php image rotation script.
> 
> <?php 
> #random images example
> #this is your file
> $file = "rotating-images.txt";
> #open the file
> $fp = file($file); 
> #generate a random number
> srand((double)microtime()*1000000); 
> #get one of the entries in the file
> $random_image = $fp[array_rand($fp)]; 
> #display the entry
> echo "<img src='$random_image'></img>"; 
> ?>

Try this version of the same script, it might solve your problem :

<?php

   $file = "rotating-images.txt";
   if (!file_exists($file))
   {
      die('Sorry, but '.$file.' does not exist');
   }
   if (!is_readable($file))
   {
      die('Sorry, but '.$file.' is not readable by php. Check permissions');
   }
   //This should remove any blank lines
   $entries = file($file);
   $random_image = trim($entries[array_rand($entries)]);
   if (strlen($random_image) > 0)
   {
     //Non-blank entry -- let check if the image exists
     if (file_exists($random_image))
     {
        echo '<img src="'.$random_image.'">';
     }
   }
?>



> 
> 
> in my rotating-images.txt file I have my list of images named
> image1.jpg, image2.jpg through image7.jpg all on a separate line.
> 
> the problem I am having Is a get a blank broken image every third or
> so rotation and I can't debug to see which image is not displaying,
> because I am calling it through <?php acquire and right
> clicking/properties doesn't tell me anything.
> 
> all the images are on the server, and in the same file as the
> rotating-images.txt
> 
> any ideas to what may be going on?



More information about the thelist mailing list