简体   繁体   English

从php提供图像时如何防止脚本名称附加到图像文件扩展名

[英]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.

相关问题 PHP:如何使用gethostbyname()防止nslookup将域附加到服务器-FQDN - PHP: how to prevent domain from being appended to the server-FQDN by nslookup using gethostbyname() 提交给自己时防止执行PHP脚本 - Prevent PHP script from being executed when submitting to self 如何在php中命名上传的文件,以防止它们被覆盖? - How to name uploaded files in php to prevent them from being overwritten? 防止PHP脚本被淹没 - Prevent PHP script from being flooded PHP 从 URL 剥离扩展名和图像尺寸中获取文件名 - PHP get file name from URL stripping extension and image dimesion 如何防止Angular表单在无效时发送到php文件(method =“post”)? - How to prevent Angular form from being sent to a php file (method="post") when invalid? 从PHP扩展获取运行脚本的名称 - Get the name of running script from a PHP extension 当文件包含在include()中时,如何防止使用变量 - How to prevent variables from being used when a file is include() ed 运行我的自定义php脚本文件时如何防止wordpress加载 - how to prevent from wordpress load when running my custom php script file 如何在PHP中以最佳方式从URL获取带有扩展名的文件名? - How to get file name with extension from the URL in an optimum way in PHP?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM