使用 curl 运行 下载文件 sftp 内存不足

download file sftp with curl running out of memory

如何避免这个错误

Fatal error: Allowed memory size of 209715200 bytes exhausted

文件很大(超过 5GB)

$host = 'domain.com';
$pass = 'xxx';

$remote = "sftp://root:$pass@$host/var/www/test.txt";
$local = 'C:\wamp\www\test.txt';

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $remote);
curl_setopt($curl, CURLOPT_PROTOCOLS, CURLPROTO_SFTP);
curl_setopt($curl, CURLOPT_USERPWD, "root:$pass");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
file_put_contents($local, curl_exec($curl));
curl_close($curl);

看看CURLOPT_FILE option。它将数据直接写入给定文件。