You have to make sure no output is made before the image headers are sent, not even one character or it will fail.
I'm not exactly sure when or why the "headers_sent" function is not working correct, but usually if you see the filename in the output something was wrong before the images was rendered in the browser.
Here is a simplified version of a script that I've used on an application, I don't advise you to use the "exit" messages, but instead maybe just trigger an error message and continue with the next step of the script.
The following example is just to make you an ideea on how to address this problem. It's checking a lot of things before the image is drawn, and it uses the GD + libJPEG libraries to display the image. I suggest this over you method.
PHP Code:
<?php
//Do not show warnings and notices
error_reporting (E_ALL ^ E_WARNING ^ E_NOTICE);
//Remove these 2 lines when going online
error_reporting (E_ALL); //Debug
ini_set ('display_errors', true); //Debug
$imageFilename = 'test.jpg';
//Check if GD library is loaded
if (!extension_loaded ('gd'))
{
exit ('No GD library available!');
}
$gdInfo = gd_info();
//Check for JPEG support
if (!$gdInfo['JPG Support'])
{
exit ('No JPEG library available! <code>libjpeg</code>');
}
//Check if headers were allready sent
//Doesn't make any sens to continue if they were sent
if (!headers_sent())
{
//Check if file exists
if (!file_exists ($imageFilename))
{
exit ('File does not exist!');
}
//Check if file is readable
if (!is_readable ($imageFilename))
{
exit ('File is not readable, please change permissions!');
}
//Attempt to open image
$image = @ imagecreatefromjpeg ($imageFilename);
if (!$image)
{
//Could not load image
exit ('Error loading image!');
}
//Send headers
header ('Content-transfer-encoding: binary');
header ('Content-Type: image/jpeg');
//Draw image
@ imagejpeg ($image);
//Destroy image
@ imagedestroy ($image);
}
else
{
//Do not make any other output to the browser
//before image is rendered
die ('Headers were allready sent!');
}
?>
Make sure you remove these lines when going online with the script, they are ment to be there just for debugging:
PHP Code:
//Remove these 2 lines when going online
error_reporting (E_ALL); //Debug
ini_set ('display_errors', true); //Debug
Hope it helps!
Boby