繁体   English   中英

Curl 忽略 CURLOPT_FILE

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM