3

当我使用 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);
4

1 回答 1

2
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();
于 2012-09-01T18:31:18.487 回答