PHP 标头 - 内容类型:图像/jpeg - 不适用于 Internet Explorer
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14148586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Header - Content-type: image/jpeg - Not working for Internet Explorer
提问by JCastell
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.
在构建 HTML 模板或修改网站时,我们都讨厌 Internet Explorer。好吧,我最近构建了一个 PHP 图像脚本来隐藏 URL 的位置。它适用于 Firefox、Chrome 甚至 Safari。
Internet Explorer refuses to display the image from the PHP script. It does not even give the broken image icons. Simply blank squares.
Internet Explorer 拒绝显示来自 PHP 脚本的图像。它甚至不提供损坏的图像图标。简单的空白方块。
Android also has the same issue, but I can get to that another time and might be related.
Android 也有同样的问题,但我可以再谈一次,可能是相关的。
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.
我在谷歌和这个网站上搜索了高低。找不到解释 Internet Explorer 不显示图像的可靠来源。
Any help is greatly appreciated.
任何帮助是极大的赞赏。
回答by kokx
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:
Content-Type 标头名称以大写 T 书写。我不确定这是否是问题所在,但某些浏览器在使用小写 t 书写时可能无法识别 Content-Type 标头。因此,您应该使用:
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.
其他可能有问题的地方是,当您尝试显示不是 jpeg 而是 png 或 gif 的图像时,同时提供了 image/jpeg 内容类型标题。因此,您应该确保为浏览器提供正确的内容类型。
回答by Jordi Kroon
Internet explorer uses the mime type image/pjpeg. You use pjpegfor IE and jpegfor other browsers.
Internet Explorer 使用 mime 类型image/pjpeg。您pjpeg用于 IE 和jpeg其他浏览器。
header("Content-Type: image/pjpeg");
Source: image/pjpeg and image/jpeg
回答by Konr Ness
Set the content length header.
设置内容长度标题。
header("Content-Length: " . filesize($imagepath));
回答by Renato Dantas
I think i know what is the problem!
我想我知道问题出在哪里了!
IE expect you to use image/jpeg and not image/jpg. try this: Header("Content-Type: image/jpeg");
IE 希望您使用 image/jpeg 而不是 image/jpg。试试这个: Header("Content-Type: image/jpeg");
in fact all browsers accpet this way! you don't have to worry anymore!
事实上,所有浏览器都以这种方式接受!你不必再担心了!

