Random (?) error with getimagesize()

Hello all,
I’m quite new to PHP but I don’t think this is a newbie error… apologies if it is, though! I have two HTML pages (addresses as follows) which call a third page, display.php (code follows). In the error log, I’m periodically getting the following error.

It doesn’t happen all the time; I’ve tested every link available and I’ve once generated it myself but couldn’t repeat it. Clearly the problem is that the path newphotos/.jpg doesn’t exist - but I can’t see how it can ever be passed to getimagesize as a parameter! Any help to solve the problem or how I could trace it would be appreciated!

Thanks.
Stuart.

Error:
[07-May-2013 08:37:48 UTC] PHP Warning: getimagesize(newphotos/.jpg) [function.getimagesize]: failed to open stream: No such file or directory in /home/lavenham/public_html/airfield/display.php on line 75

HTML pages:
www.lavenham.co.uk/airfield/
www.lavenham.co.uk/airfield/fieldmap.htm

Code from display.php page:
[php]

<?php $ref=$_GET['ref']; //Set up array of captions $pictures=array( array( caption=>"An enlisted men's ablution hut", file=>"a"), array( caption=>"The remains of a shower block", file=>"b"), array( caption=>"The track to Sites 5, 6 & 7, looking South", file=>"c"), array( caption=>"The track to Sites 5, 6 & 7, looking South", file=>"d"), array( caption=>"Road to North of Site 5", file=>"e"), array( caption=>"Pumphouse, Site 5", file=>"f"), array( caption=>"Burma Road, towards Lavenham", file=>"g"), array( caption=>"Burma Road, towards Airfield", file=>"h"), array( caption=>"Gymnasium, Site 2", file=>"i"), array( caption=>"Squadron Offices, Technical Site", file=>"j"), array( caption=>"North side of Bomb Stores", file=>"k"), array( caption=>"Runway 2, looking NE", file=>"l"), array( caption=>"Runway 3", file=>"m"), array( caption=>"South, from SE end of Runway 3", file=>"n"), array( caption=>"The Control Tower (currently being restored)", file=>"o"), array( caption=>"Memorial plaque in Lavenham Market Place", file=>"p")); ?> <?php print $pictures[$ref][caption]; ?>

 
<?php //Get the size of the picture $imgsize=getimagesize("newphotos/".$pictures[$ref][file].".jpg"); //output the img tag print ("\"[");
print"); //display the caption print ("
\n"); print $pictures[$ref][caption]; //calculate the index number for the next and previous images $prev=$ref-1; $next=$ref+1; //set the total number of photos $totalphotos=16; //display the text for the Market Place plaque if ($ref==15) { print("

MEMORIAL"); print("
DEDICATED TO THE MEN OF THE"); print("
487TH BOMB GROUP(H)"); print("
WHO SACRIFICED THEIR LIVES IN WORLD WAR II, THAT THE IDEALS"); print("
OF DEMOCRACY MIGHT LIVE"); } ?>



<?PHP //code to display previous button if ($prev>=0) { print ("\"[<"); } else { //code to display greyed out previous button print ("\"\""); } ?>

[Close Window]

<?PHP //code to display next button if ($next<>$totalphotos) { print ("\"[Next"); } else { //code to display greyed out next button print ("\"\""); } ?>




 

[/php]

The error is on this line:

[php]$imgsize=getimagesize(“newphotos/”.$pictures[$ref][file].".jpg");[/php]

As the error message suggests, the file it tries to open does not exist. As such, you’ll need to put debugging in there in order to find out exactly what $picture[$ref][‘file’] actually points to. If you want to get rid of the error altogether, you can check if file_exists(“newphotos/”.$pictures[$ref][file].".jpg") returns true - if it does, the file exists!

Thanks for the very prompt reply, Seb. I could use the file_exists to suppress the error but since the files don’t change, there’s no reason to have an error at all. Hence I’m trying to debug.

So I’ve added the following code. Once it works, I’ll add ==false to the if statement so that only errors are logged. However my problem now is that it only logs the value of $pictures[$ref][file] if this PHP page is called from one of the HTML pages. If it’s called from itself (using the Next or Previous buttons), it doesn’t log it. Any suggestions about what I’m doing wrong?

Thanks again.
Stuart.

[php]//temporary code to store the contents of $pictures[$ref][file]
if (file_exists(“newphotos/”.$pictures[$ref][file].".jpg")) {
$temp_file = ‘temp.txt’;
// Open the file to get existing content
$current = file_get_contents($temp_file);
// Append a new person to the file
$current .= $pictures[$ref][file];
// Write the contents back to the file
file_put_contents($temp_file, $current);
}[/php]

Your code, simplified and fixed:

[php]//temporary code to store the contents of $pictures[$ref][file]
if (!file_exists(“newphotos/”.$pictures[$ref][“file”].".jpg")) {
$temp_file = ‘temp.txt’;
$file = fopen($temp_file,“a”);
fwrite($file, $pictures[$ref][“file”]." - ref: “.$ref.”\r\n");
fclose($file);
}[/php]

This will be more useful as this will log what has NOT been found (! means not).

Seb, thanks once again. Yes, your ! is much more elegant than the ==false that I was suggesting! I’ve put the code live so now I’ll see what happens. (The page is low-traffic so it can take a day or two before any errors are generated.)

Stuart.

I’m back again as I’ve now got some new error messages (which took a while to be generated and then I forgot to check). In the log file, each line is simply:

  • ref:

So if I understand correctly, that means that $ref is empty and hence $pictures[$ref][“file”] isn’t valid? My only guess as to why this might be happening is that the request is interrupted. So the user clicks on one of the links and then immediately clicks on the browser Stop button or another link. Or could it be a pop-up blocker causing a problem… but on reflection, no: in that case the PHP page wouldn’t get called at all so couldn’t generate an error.

Any ideas would be appreciated before I give up on trying to fix this error!

Thanks.
Stuart.

Sponsor our Newsletter | Privacy Policy | Terms of Service