如何在“另存为”对话框中强制使用正确的文件扩展名?(.jpg而不是.php)


How to force the proper file extension in the "save as" dialog? (.jpg instead of .php)

要用密码保护网站图像,我通过PHP访问它们。使用网址"meme_penguin.jpg"我使用"image.php?file=penguin"。这非常适合显示它,除非用户尝试"另存为"。

在 chrome 中,"另存为"对话框将建议文件类型为 PHP 而不是 JPG在IE浏览器中,它会建议BMP

显然,用户只需更改文件类型并重命名文件即可。但是,这不是一个优雅的解决方案,肯定会引起问题。

我们如何以适当的扩展名JPG自动"另存为"?有没有其他方法?- 非常感谢!!

找到了!我缺少第三行代码。您可以使用它在页面中显示安全图像,或将其下载为 jpg从堆栈溢出中的另一个问题中获取前两行

//need this two lines to work properly on IE8
header("Pragma: "); 
header("Cache-Control: ");
//was missing this line
header('Content-Disposition: attachment; filename="renamed.jpg"');
header("Content-Type: image/jpeg");
echo file_get_contents("images/somepic.jpg");

从 php.net

将内容类型标头设置为正确的值作为响应

http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html14.17 内容类型

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