当我使用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);
答案 0 :(得分: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();