【问题标题】:Problem with save image file in src在 src 中保存图像文件的问题
【发布时间】:2011-05-18 08:52:45
【问题描述】:

在image.php中,代码喜欢:

header("Content-Type:image/png");
$file = "img.png";
readfile($file); 

在其他文件中,代码为:

<img src="image.php">

当我从浏览器右键单击图像另存为时,它默认保存为image.php。我想将它保存为 img.png,有什么想法吗?

【问题讨论】:

    标签: php image src


    【解决方案1】:

    您也可以将此标头添加到您的 PHP 文件中:

    header('Content-Disposition: attachment; filename="image.png"');
    

    【讨论】:

      【解决方案2】:

      这可以通过使用 URL 重写来实现。

      在您的 .htaccess 中,添加类似这样的内容(假设 mod_rewrite 已启用并且您使用的是 Apache):

      RewriteRule ^image.png$ /image.php [L]
      

      您的 HTML 将随文件名更改为:

      <img src="image.png" />
      

      【讨论】:

      • 图片由PHP文件生成。
      • 正确。 URL 重写将 'image.png' 映射到 'image.php'... image.php 在这里被调用。
      猜你喜欢
      • 2020-11-08
      • 1970-01-01
      • 1970-01-01
      • 2012-10-28
      • 2010-12-29
      • 1970-01-01
      • 2011-11-26
      • 2017-10-24
      • 2013-05-27
      相关资源
      最近更新 更多