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);
}
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With