开发者

Best method to download remote files

开发者 https://www.devze.com 2023-03-06 01:37 出处:网络
I\'ve been trying to find the best method to download large files from other servers using PHP , but seems i am failing in that or i am not fully satisfied .

I've been trying to find the best method to download large files from other servers using PHP , but seems i am failing in that or i am not fully satisfied .

so my question is , what is the fastest method that uses less ram to download large files ? is it curl ? of fopen ? and if it was fopen , what strategy to use ?

Thank开发者_JAVA百科 you .


Fastest would probably be using sockets, but that would be like inventing the wheel again. You should indeed use cURL. I found this snippet online, so that you dont use all your memory:

set_time_limit(0);

$fp = fopen (dirname(__FILE__) . '/file.ext', 'w+'); // Output file
$ch = curl_init('http://www.example.com/largefile.ext'); // Input file
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
curl_close($ch);
fclose($fp);

Source: http://www.webdigity.com/index.php?action=tutorial;code=45


fopen is not reliable in that it requires you to have allow_url_fopen enabled (it's usually disabled due to security concerns). You have far more options with cURL than you do with fopen.

0

精彩评论

暂无评论...
验证码 换一张
取 消