【问题标题】:PHP download image using headerPHP使用标题下载图像
【发布时间】:2015-12-03 14:38:46
【问题描述】:

总的来说,我对 php 和 web 编码很陌生。我正在尝试提供通过 php 下载的图像。我需要什么额外的代码才能做到这一点?我试过用谷歌搜索它并在这里搜索,虽然我知道这个问题之前已经被问过和回答过,但阅读答案让我没有经验的头脑旋转。如果有人可以帮助我或指出正确的方向,将不胜感激!

public function ExportUserImage()
    {
        $image = $this->user->image;

        header("Content-type: image/jpeg");  
        header("Cache-Control: no-store, no-cache");  
        header('Content-Disposition: attachment; filename="avatar.jpg"');
        $outstream = fopen($image,'w'); // Not sure if this is right 
        //>>don't know what I need here<<
        fclose($outstream);

    }

【问题讨论】:

  • echo($image)echo($outstream)应该可以解决问题。
  • 如果我使用echo($image),是否需要outstream / fopen? - 也感谢您的回复
  • 您想下载位于同一文件夹中的图像 avatar.jpg?我说的对吗?
  • 不,我只想下载带有文件名 avatar.jpg 的选择器的图像
  • @AlecGamble 如果 ècho($image)` 工作你不需要它

标签: php download header jpeg


【解决方案1】:

您只是错过了实际发出应该下载的图像流。

您可以简单地回显 $image,无需为其打开文件流,因为您正确设置了标题。

public function ExportUserImage()
    {
        $image = $this->user->image;

        header("Content-type: image/jpeg");  
        header("Cache-Control: no-store, no-cache");  
        header('Content-Disposition: attachment; filename="avatar.jpg"');
        echo $image;

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-30
    • 2011-05-27
    • 1970-01-01
    • 1970-01-01
    • 2016-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多