简体   繁体   中英

Curl ignores CURLOPT_FILE

I want to download file with CURLOPT_FILE option.

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

I expect, that file would be saved in $file, but $file is empty, and curl_exec returns content of the file in $res. So, i must use file_put_contents($file_name, $res) to save the file.

But I want to download BIG files (1Gb and more), and, as I read, downloading whith CURLOPT_FILE is more memory efficient. With file_put_contents my script can run out of memory.

So, why curl not saving file to $file? I use PHP 7.3.9 and I have such problem both on Windows and Linux OS.

From the notes of curl_setopt ( https://www.php.net/manual/en/function.curl-setopt.php#99082 ) it looks as though you should have the CURLOPT_RETURNTRANSFER before the CURLOPT_FILE option...

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FILE, $file);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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