A nasty(!) PHP "gotcha" with "imagejpeg()"
I recently swatted a bug that admittedly had me bamfoozled for quite some time. The problem was that PHP code ... code which had been running flawlessly for years ... suddenly stopped producing usable images. Everything that it produced was corrupt.
Here's an excerpt of the broken code: Code:
<?php This code is intended to produce only the output of the imagejpeg() function, with an HTTP header that specifies that the data is an image. So far, so good. But... here's the bug: There is an extra newline ($0A) character that is added by the blank line between the two <?php tags at the start of the file. The browser sees this data, including the unwanted newline byte, as a (corrupt) image. If this blank line is removed, the newline disappears and the code works correctly. Like this: Code:
<?php |
Hi
I've seen worse. Sometimes you include some other files, and they have a newline after the last ?> marker. Other times, somebody's editor adds a UTF-8 BOM in the file you include, and those you normally cannot see. A safer way is to use output_buffering in your php.ini: Quote:
|
Quote:
|
All times are GMT -5. The time now is 07:04 PM. |