有个需求,想实现PHP里用php_curl下载大文件,如果直接下载到内存呢,那肯定药丸,所以肯定要输出到文件才行,示例代码如下:

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

以上示例代码完整参考:Downloading a large file using curl

Related Posts: PHP 如何使用php_curl下载大文件 :

avatar