[英]Using PHP to force image download doesn't work correctly
I have an image link that is 100% fine. 我有一个100%精细的图像链接。 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.
但是,每当我尝试使用PHP的标头来强制下载图像时,它都不会起作用。 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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.