PHP使用标题下载图像

Ale*_*ble 4 php jpeg header download

总的来说,我真的是php和web编码的新手。我正在尝试提供通过php下载的图片。我需要什么其他代码才能实现此目的?我已经尝试过使用Google搜索,并在此处进行搜索,尽管我知道之前已经有人问过这个问题,但阅读答案却使我的经验不足。如果有人可以帮助我或指出正确的方向,将不胜感激!

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);

    }
Run Code Online (Sandbox Code Playgroud)

Kho*_*oly 5

您只是错过了实际提供的图像流,应该下载该图像流。

您可以简单地回显$ 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;

    }
Run Code Online (Sandbox Code Playgroud)