2012-09-01 333 views
2

當我使用cURL下載某個文件時,cURL會將文件打印到瀏覽器。但是我想打開'保存到'瀏覽器下載窗口。我怎樣才能做到這一點?cURL +通過瀏覽器下載文件

$c = curl_init(); 
curl_setopt($c, CURLOPT_URL, $url); 
curl_setopt($c, CURLOPT_POSTFIELDS, "file_name=test.rar"); 
curl_setopt($c, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($c, CURLOPT_FOLLOWLOCATION, true); 
curl_exec($c); 
curl_close($c); 

回答

3
ini_set('memory_limit','128M'); 
$content = curl_exec($c); 
curl_close($c); 

header('Content-Description: File Transfer'); 
header('Content-Type: application/octet-stream'); 
header('Content-Disposition: attachment; filename=your_file.txt'); 
header('Content-Transfer-Encoding: binary'); 
header('Expires: 0'); 
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
header('Pragma: public'); 
header('Content-Length: ' . strlen($content)); 
ob_clean(); 
flush(); 
echo $content; 
flush(); 
+0

我越來越內存泄漏致命錯誤:67108864個字節允許內存大小耗盡 –

+0

使用'的ini_set( 'memory_limit的', '128M');' – Peter

+0

所以,如果我的理解是正確的,捲曲的意志下載文件後,我得到了'保存到'窗口?這是爲什麼? –