|
| | |
| Main » 2012 » November » 6 » Remote File Download using Curl
10:10 PM Remote File Download using Curl |
cURL is a great tool to help you connect to remote web sites, making it
easy to post forms, retrieve web pages, or even to download files.Now
I'll show you how you can download a file straight to disk using cURL.
Example 1)
<?php
$url = 'http://www.example.com/a-large-file.zip';
$path
= '/path/to/a-large-file.zip';
$ch = curl_init($url);
curl_setopt
($ch, CURLOPT_RETURNTRANSFER, true);
$data
= curl_exec($ch);
curl_close($ch);
file_put_contents($path, $data);
?>
There is however a problem with this code.
If the file you're downloading is quite large, the entire contents must
be read into memory before being written to disk. Doing so can result in
your script breaking down due to exceeding the memory limit.
example 2)
<?php
$url = 'http://www.example.com/a-large-file.zip';
$path = '/path/to/a-large-file.zip';
$fp = fopen($path, 'w');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fp);
$data = curl_exec($ch);
curl_close($ch);
fclose($fp);
?>
That's all there is to it. You can now download files without worrying about exceeding PHP's memory limit.
|
Category: Web Development |
Views: 330 |
Added by: admin-priyank
| Rating: 5.0/1 |
| |
| | |
|
Calendar |
| « November 2012 » | Su | Mo | Tu | We | Th | Fr | Sa | | | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 |
| |
|
Our poll |
| | |
|
Statistics |
|
Total online: 1 Guests: 1 Users: 0 | |
|
|