![](/img/trans.png)
[英]php Curl confilict CURLOPT_FILE and CURLOPT_RETURNTRANSFER in docker
[英]Curl ignores CURLOPT_FILE
我想用 CURLOPT_FILE 選項下載文件。
$file_name = 'm1.dat';
$file = fopen($file_name, 'w');
$ch = curl_init($download_url);
curl_setopt($ch, CURLOPT_FILE, $file);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HEADER, false);
$res = curl_exec($ch);
curl_close($ch);
fclose($file);
我希望,該文件將保存在 $file 中,但 $file 是空的,並且 curl_exec 在 $res 中返回文件的內容。 所以,我必須使用 file_put_contents($file_name, $res) 來保存文件。
但我想下載 BIG 文件(1Gb 及更多),並且,正如我所讀,使用 CURLOPT_FILE 下載 memory 效率更高。 使用 file_put_contents 我的腳本可以用完 memory。
那么,為什么 curl 不將文件保存到 $file? 我使用 PHP 7.3.9 並且在 Windows 和 Linux 操作系統上都有這樣的問題。
從 curl_setopt 的注釋( https://www.php.net/manual/en/function.curl-setopt.php#99082 )看來,您應該在CURLOPT_RETURNTRANSFER
選項之前使用CURLOPT_FILE
...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FILE, $file);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.