[英]Using PHP to force image download doesn't work correctly
我有一個100%精細的圖像鏈接。 我可以用它鏈接到圖像,也可以用它顯示圖像。 但是,每當我嘗試使用PHP的標頭來強制下載圖像時,它都不會起作用。 這是我正在使用的:
$file = $link;
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Length: ' . filesize($file));
readfile($file);
下載開始,並詢問我應將圖像保存在何處。 但是,下載圖像后,它根本無法打開。 我在這里做錯了什么?
您需要先下載圖像:
$file = 'newimage.png';
$file_contents = file_get_contents($link);
file_put_contents($file, $file_contents);
然后您可以輸出圖像:
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.