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.
精彩评论