我正在使用Apache的mod_rewrite
将请求的JPG文件路由到我的Web根目录之外的目录。
它一般很好,但有一些图片不显示。 然后我意识到,当我在图像URL上使用PHP的get_headers()
函数时,它们全部返回
Content-Type: text/html; charset=UTF-8
Content-Type: text/html; charset=UTF-8
而不是正确的image/jpeg
标题types。
我已经明确地设置了Content-Type: image/jpeg
头文件,并且仍然没有任何图像返回正确的头文件 – 虽然大多数文件都能正确显示,但是我不知道为什么。
我怎样才能保证一个JPG文件通过mod_rewrite
redirect时发送正确的头文件?
这是你可以做的。 创建一个PHP文件,将获得正确的文件,并通过它
<?php $sImage = 'imagename.jpg'; header("Content-Type: image/jpeg"); header("Content-Length: " .(string)(filesize($sImage)) ); echo file_get_contents($sImage);
要么
<?php $sImage = 'imagename.jpg'; $rFP = fopen($sImage, 'rb'); header("Content-Type: image/jpeg"); header("Content-Length: " .(string)(filesize($sImage)) ); fpassthru($rFP); exit;
你也可以用T
标志来设置Content-Type头域的mod_rewrite:
RewriteRule … … [T=image/jpeg]
怎么样的图片是不是JPG。 像.gif一样,…
您需要使用mime_content_type() (不建议使用)或fileinfo扩展名来确定要发送的内容类型。
编辑:我不建议这样做,但如果您正在使用文件扩展名的特定子集,也可以创建一个小型字典的内容类型数组,并使用文件扩展名来确定发送哪一个。