[英]How to prevent script name from being appended to image file extension when serving images from php
I have an html with an img tag like this我有一个像这样带有 img 标签的 html
<img src="loadImg.php" alt="User Image">
The loadImg.php serves the image in this way loadImg.php 以这种方式提供图像
...
header("Content-Type: image/jpeg");
imagejpeg($img);
imagedestroy($img);
?>
The image is served on the html but when you right click to save the image it has the script name and extension该图像在 html 上提供,但是当您右键单击以保存图像时,它具有脚本名称和扩展名
In this case loadImg.php.jpg在这种情况下 loadImg.php.jpg
I would like the file name not to include the script name end result being something like profilePic.jpg我希望文件名不包含脚本名称最终结果类似于 profilePic.jpg
How can i achieve this?我怎样才能做到这一点?
You could use the Content-Disposition HTTP header: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition您可以使用 Content-Disposition HTTP 标头: https : //developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition
For example:例如:
...
$file = 'user-image.jpg';
header('Content-Disposition: attachment; filename="'.basename($file).'"');
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.