PHP Header - Content-type: image/jpeg - Not Working for Internet Explorer
When serving dynamic images via a PHP script, you might encounter a frustrating scenario where modern browsers like Firefox, Chrome, and Safari render the file perfectly, but Internet Explorer leaves you with blank squares instead of broken image icons. This specific behavior often points to how the browser interprets the Content-Type header combined with the file extension strategy used by legacy rendering engines.
The core issue in your script lies in the direct correlation between the HTTP header and the actual file data being served. Your code sets header("Content-type: image/jpeg"); and immediately echoes the raw bytes of the file stored at $imagepath. For this to work, the file referenced by $imagepath must be a valid JPEG binary stream. If the database contains a path to a GIF or PNG file, but you force the header to claim it is a JPEG, modern browsers are generally forgiving enough to attempt rendering or simply show the mismatch as an error. Internet Explorer, however, historically adhered more strictly to MIME type declarations when resolving image sources.
The script relies on the mysql_fetch_array function, which indicates you are using the legacy MySQL extension rather than PDO or MySQLi. While this isn't the direct cause of the rendering failure, it is a significant security vulnerability. If $image_id comes directly from $_GET['id'], your application is susceptible to SQL injection. An attacker could inject malicious payloads into the query string to access other rows in the database or execute arbitrary commands. Before addressing the IE rendering issue, you must sanitize this input using prepared statements.
$stmt = $pdo->prepare("SELECT path FROM images WHERE code = :id");
$stmt->execute([':id' => $_GET['id']]);
$row = $stmt->fetch();
$imagepath = $row['path'];
Assuming the file at $imagepath is indeed a valid JPEG, the issue might stem from how Internet Explorer handles the MIME type declaration in conjunction with the request method or caching mechanisms. IE sometimes struggles with images served directly via echo file_get_contents() if the underlying stream does not match the expected signature strictly, or if the browser's internal cache for that specific URL has a corrupted entry.
To resolve this without rewriting your entire architecture, you can try explicitly forcing the browser to bypass its cache by appending a version parameter to the output or ensuring the Content-Length header is calculated correctly based on the file size. However, the most robust fix often involves verifying that the actual file extension in the URL matches the declared MIME type. If your PHP script serves a JPEG but the URL ends in .gif, IE will refuse to display it.
If you are using a framework like Laravel, this problem is largely abstracted away because the router and response helpers manage MIME types automatically based on file extensions or explicit response()->file() calls. For raw PHP scripts, ensure that the Content-Type header is sent before any output, including whitespace, and that the file path resolves to an actual image file. If the database stores paths to non-image files, you must validate the content type of the file itself before sending the header:
$imageType = mime_content_type($imagepath);
if ($imageType === 'image/jpeg') {
header("Content-type: image/jpeg");
echo file_get_contents("$imagepath");
} else {
// Handle mismatch or error
}
By validating the actual MIME type of the stored file before declaring it, you ensure consistency between the HTTP header and the binary data. This approach prevents IE from rejecting the stream due to a mismatch, effectively resolving the blank square issue while maintaining compatibility across all browsers.