Site icon 时鹏亮的Blog

PHP 如何使用php_curl下载大文件

请知悉:本文最近一次更新为 5年 前,文中内容可能已经过时。

有个需求,想实现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


如您从本文得到了有价值的信息或帮助,请考虑扫描文末二维码捐赠和鼓励。

尊重他人劳动成果。转载请务必附上原文链接,我将感激不尽。


与《PHP 如何使用php_curl下载大文件》相关的博文:

Exit mobile version