2013-07-31 30 views
0

我從網址下載形象問題,下載圖像,而不在我的服務器文件夾中保存

$url = 'http://example.com/image.php'; 
$img = '/my/folder/flower.gif'; 
file_put_contents($img, file_get_contents($url)); 

在我的服務器文件夾中。但我需要在沒有我的服務器節能節電它需要下載給用戶。

+0

[看這裏] [1]是一個幫助你的問題 [1]:http://stackoverflow.com/questions/5648967/force-download-image –

回答

0

使用application/octet-stream代替圖像/ JPG:

如果[內容處置]報頭在與所述應用程序/八位字節流內容類型的響應中使用的,隱含的建議是,用戶代理不應該顯示響應,但直接輸入'保存響應爲...'對話框。 — RFC 2616 – 19.5.1 Content-Disposition

EDIT 

function forceDownloadQR($url, $width = 150, $height = 150) { 
    $url = urlencode($url); 
    $image = 'http://chart.apis.google.com/chart?chs='.$width.'x'.$height.'&cht=qr&chl='.$url; 
    $file = file_get_contents($image); 
    header("Content-type: application/octet-stream"); 
    header("Content-Disposition: attachment; filename=qrcode.png"); 
    header("Cache-Control: public"); 
    header("Content-length: " . strlen($file)); // tells file size 
    header("Pragma: no-cache"); 
    echo $file; 
    die; 
} 

forceDownloadQR('http://google.com'); 

,如果你想下載文件到您的Web服務器(並保存),只需使用copy()

copy($url, 'myfile.png'); 
+0

無法正常工作。<?php $ url ='http://chart.googleapis.com/chart?cht= QR&CHS = 150×150&CHL = asasdasdsasd&崔= UTF-8&CHLD = H | 0' ; header('Content-type:application/octet-stream'); header('Content-Disposition:attachment; filename:image.png'); echo file_get_contents($ url); ?> – elango

+0

查看編輯的代碼 –

0

設置正確的頭和echo出來,而不是file_put_contents的

$url = 'http://example.com/image.php'; 

header('Content-Disposition: attachment; filename:image.jpg'); 
echo file_get_contents($url); 
+0

不工作.. $ url ='http://chart.googleapis.com/chart?cht=qr&chs=150x150&chl=asasdasdsasd&choe=UTF-8&chld=H|0'; header('Content-Disposition:attachment; filename:image.png'); echo file_get_contents($ url); – elango

相關問題