php mod_rewrite 后 jpg 文件的 HTTP 标头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1558753/
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
HTTP headers for jpg files after mod_rewrite
提问by Brian
I'm using Apache's mod_rewriteto route requests for JPG files to a directory outside my web root.
我正在使用 Apachemod_rewrite将 JPG 文件请求路由到我的 Web 根目录之外的目录。
It generally has been fine, but there are a few images that do not display. I then realized that when I use PHP's get_headers()function on my image URLs, they are all returningContent-Type: text/html; charset=UTF-8instead of the proper image/jpegheader types.
总体来说没问题,但有一些图像不显示。然后我意识到当我get_headers()在我的图像 URL 上使用 PHP 的函数时,它们都返回Content-Type: text/html; charset=UTF-8而不是正确的image/jpeg标题类型。
I have tried explicitly setting the Content-Type: image/jpegheader and still, none of my images return the correct headers - although most do display correctly, but I'm not sure why.
我已经尝试明确设置Content-Type: image/jpeg标题,但仍然没有我的图像返回正确的标题 - 尽管大多数都能正确显示,但我不确定为什么。
How can I assure a JPG file is sent with the correct header when redirecting via mod_rewrite?
如何确保在通过 重定向时使用正确的标头发送 JPG 文件mod_rewrite?
回答by Robert Cabri
This is what you could do. Create a PHP file that will get the right file and passes it through
这是你可以做的。创建一个 PHP 文件,它将获取正确的文件并通过它
<?php
$sImage = 'imagename.jpg';
header("Content-Type: image/jpeg");
header("Content-Length: " .(string)(filesize($sImage)) );
echo file_get_contents($sImage);
or
或者
<?php
$sImage = 'imagename.jpg';
$rFP = fopen($sImage, 'rb');
header("Content-Type: image/jpeg");
header("Content-Length: " .(string)(filesize($sImage)) );
fpassthru($rFP);
exit;
回答by Gumbo
You can also set the Content-Typeheader field with mod_rewrite with the Tflag:
您还可以使用带有标志的mod_rewrite设置Content-Type标头字段T:
RewriteRule … … [T=image/jpeg]
回答by simshaun
How about image which is not.jpg. Like .gif, ...
不是.jpg的图像怎么样。像.gif,...
You'll need to use mime_content_type()(which is deprecated) or the fileinfoextension to determine which content-type to send.
您需要使用mime_content_type()(已弃用)或fileinfo扩展名来确定要发送的内容类型。
Edit: I don't recommend this, but if you are working with a specific subset of file extensions, you could also create a small dictionary array of content-types and use the file extension to determine which one to send.
编辑:我不建议这样做,但如果您正在使用文件扩展名的特定子集,您还可以创建一个内容类型的小字典数组,并使用文件扩展名来确定要发送哪个。

