Rodrigo
Rodrigo

Reputation: 5119

php readfile() strange error message

showIMG.php is used to show an image from its path+filename, and contains

<?php
$fname = $_GET['file'];
$size = getimagesize($fname);
$mime = $size['mime'];
header("Content-type: $mime");
readfile($fname);
?>

I'm calling it from another file like that

$cell = "filename.png";
$size = getimagesize($cell);
echo "<img src='showIMG.php?file=$cell' /> $size[3]";

$size[3] is displaying the correct image dimensions, what means the file was found. But the image don't appear, only a broken image icon. If I copy the location of that icon and open in another tab in Firefox, I get the error message:

The image "http://servername/showIMG.php?file=filename.png" cannot be displayed because it contains errors.

What is very strange, since the parameter passed to readfile() was only "filename.png", not the full url. Anybody knows what is going on?

Upvotes: 1

Views: 922

Answers (2)

Marc B
Marc B

Reputation: 360602

Save/download the url, and then open the file in a hex/text editor. Look for anything out of place, e.g. php warnings. If ANYTHING is output from your showIMG.php script that ISN'T actual image data, it'll corrupt the file. Just because everything looks fine on the server doesn't mean that's what's being received on the client-side.


and given the investigation in the comments:

It's a unicode Byte-Order-Mark. You probably edited the PHP file in a unicode-aware editor and it put the BOM at the start of the script. Since that BOM is OUTSIDE of the block, it counts as output and will be inserted at the start of any file you send out. Check your editor to see if it has a "save without BOM" option.

Upvotes: 2

user2563689
user2563689

Reputation: 1

Try to add:

header('Content-Transfer-Encoding: binary');

header('Content-Length: ' . filesize($fname));

Upvotes: 0

Related Questions