So today I noticed the code for displaying scanned images was no longer working. This code basically boils down to:
header('Content-type: image/jpeg');
readfile('some_file.jpg');
Pretty simple, right? Yet somehow, even though some_file.jpg really does exist, and it really is a JPEG, I'm getting nothing. IE gives me the "image not found" red X icon, Chrome gives me a blank. What?
Rummaging around the web, I found a couple references to how unintentional whitespace output by your PHP script can screw your header() call into the ground. This seemed a likely explanation, but where (and why) is my PHP spitting out unwanted whitespace?
Turns out the culprit was lurking within these closing lines of an include:
static function asOptionalJoin($fk = 'ipc') {
$c = new static();
return CriteriaJoin::optional($c, $fk);
}
}
?>
Do you see it? Trick question; it's invisible.
static function asOptionalJoin($fk = 'ipc') {
$c = new static();
return CriteriaJoin::optional($c, $fk);
}
}
?> <— look again
Yes, a space immediately to the right of the closing ?> PHP tag was the source of my grief. Take that space away, and the header call works again and the image springs back to life.
Interesting side note: one of the aforementioned resources explains that closing ?> tags are not only optional (didn't know this), but that some frameworks actually forbid them in PHP-only source files for the very reason of keeping accidental whitespace out of the output.
Sounds good to me; they're gone.