使用curl下载大文件

我需要使用curl下载远程文件。

这是我的示例代码:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$st = curl_exec($ch);

$fd = fopen($tmp_name, 'w');

fwrite($fd, $st);

fclose($fd);

curl_close($ch);

但是它不能处理大文件,因为它首先读取内存。

是否可以将文件直接流式传输到磁盘?

回答:

<?php

set_time_limit(0);

//This is the file where we save the information

$fp = fopen (dirname(__FILE__) . '/localfile.tmp', 'w+');

//Here is the file we are downloading, replace spaces with %20

$ch = curl_init(str_replace(" ","%20",$url));

curl_setopt($ch, CURLOPT_TIMEOUT, 50);

// write curl response to file

curl_setopt($ch, CURLOPT_FILE, $fp);

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

// get curl response

curl_exec($ch);

curl_close($ch);

fclose($fp);

?>

以上是 使用curl下载大文件 的全部内容, 来源链接: utcz.com/qa/427422.html

回到顶部