Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP download image using header

I'm really new to php and web coding in general. I'm trying to offer an image for download via php. What additional code do I need to make this happen? I've tried googling it and searching on here and, whilst I know this question has been asked and answered before, reading the answers is making my inexperienced head spin. If anyone can help me or point me in the right direction, it would be greatly appreciated!

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

    }
like image 557
Alec Gamble Avatar asked Dec 03 '15 14:12

Alec Gamble


1 Answers

You just missed out to actually give out the image stream, which should be downloaded.

You can simple echo out the $image, no need to open a filestream for it, because you set your headers right.

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;

    }
like image 95
KhorneHoly Avatar answered Nov 05 '22 21:11

KhorneHoly