【问题标题】:How to prevent script name from being appended to image file extension when serving images from php从 php 提供图像时如何防止脚本名称附加到图像文件扩展名
【发布时间】:2021-01-12 01:46:32
【问题描述】:

我有一个像这样带有 img 标签的 html

<img src="loadImg.php" alt="User Image">

loadImg.php 以这种方式提供图像

...
header("Content-Type: image/jpeg");
imagejpeg($img);
imagedestroy($img);
?>

图像在 html 上提供,但是当您右键单击以保存图像时,它具有脚本名称和扩展名

在本例中为 loadImg.php.jpg

我希望文件名不包含脚本名称,最终结果类似于 profilePic.jpg

我怎样才能做到这一点?

【问题讨论】:

  • 修复你的 imagejped 函数

标签: php html image image-processing


【解决方案1】:

您可以使用 Content-Disposition HTTP 标头:https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition

例如:

...
$file = 'user-image.jpg';
header('Content-Disposition: attachment; filename="'.basename($file).'"');
...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-19
    • 2011-06-15
    • 2017-07-31
    • 2022-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多