I have an image link that is 100% fine. I can use it to link to the image, and I can use it to display the image. However whenever I try to use PHP's headers to force an image download, it doesn't work. Here's what I'm using:
$file = $link;
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Length: ' . filesize($file));
readfile($file);
The download starts and asks me where to save the image, as it should. However after the image is downloaded, it simply won't open. What am I doing wrong here?
You need to download the image first:
$file = 'newimage.png';
$file_contents = file_get_contents($link);
file_put_contents($file, $file_contents);
Then you can output the image:
header('Content-type: image/jpeg');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Length: ' . filesize($file));
readfile($file);
exit();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.