JCastell
JCastell

Reputation: 1145

PHP Header - Content-type: image/jpeg - Not working for Internet Explorer

We all hate Internet Explorer when building HTML templates, or modifying websites. Well I recently built a PHP image script to hide the location of the URL. It works fine for Firefox, Chrome and even Safari.

Internet Explorer refuses to display the image from the PHP script. It does not even give the broken image icons. Simply blank squares.

Android also has the same issue, but I can get to that another time and might be related.

Here is my code for the image script:

$image_id = $_GET['id'];

include "mysql_connect.php";
$sql = "SELECT * FROM images WHERE code='$image_id'";
$result = mysql_query($sql);
$r=mysql_fetch_array($result);

$imagepath=$r['path'];

// Produce proper Image
header("Content-type: image/jpeg");

echo file_get_contents("$imagepath");

I searched high and low on Google and this website. Could not find a solid source explaining why Internet Explorer is not displaying the image.

Any help is greatly appreciated.

Upvotes: 8

Views: 91499

Answers (4)

Renato Dantas
Renato Dantas

Reputation: 11

I think I know what the problem is.

Internet Explorer expects you to use image/jpeg and not image/jpg.

Try this:

Header("Content-Type: image/jpeg"); 

All browsers accept this format, and you won't have to worry anymore.

Upvotes: -1

Konr Ness
Konr Ness

Reputation: 2153

Set the content length header.

header("Content-Length: " . filesize($imagepath));

Upvotes: 1

Jordi Kroon
Jordi Kroon

Reputation: 2597

Internet explorer uses the mime type image/pjpeg. You use pjpeg for IE and jpeg for other browsers.

header("Content-Type: image/pjpeg");

Source: image/pjpeg and image/jpeg

Upvotes: 2

kokx
kokx

Reputation: 1706

The Content-Type header name is written with an uppercase T. I am not sure if that is the issue, but some browsers might not recognize the Content-Type header when it is written with a lowercase t. Thus, you should use:

header("Content-Type: image/jpeg");

Something else that might be a problem, is when you try to display an image that is not a jpeg, but a png or gif, while you give the image/jpeg content-type header. So, you should ensure that you give the correct content-type to the browser.

Upvotes: 12

Related Questions