gpt4 book ai didi

php - cURL + 通过浏览器下载文件

转载 作者:搜寻专家 更新时间:2023-10-31 20:46:53 34 4
gpt4 key购买 nike

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

最佳答案

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();

关于php - cURL + 通过浏览器下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12230271/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com