MiX1.TK
Site menu
Login form
Section categories
Web Development [20]
Learn about PHP & Web Development
Education [4]
Learn for Free
Knowledge & Tricks [0]
General Knowledge,computer Knowledge & tricks
Writing World [2]
A MiX World of SmS on Various Topics
Search
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: 105 | Added by: admin-priyank | Rating: 5.0/1
Total comments: 0
Name *:
Email *:
Code *:
Calendar
«  November 2012  »
SuMoTuWeThFrSa
    123
45678910
11121314151617
18192021222324
252627282930
Entries archive
Our poll
Rate my site
Total of answers: 4
Site friends
  • Create a free website
  • Online Desktop
  • Free Online Games
  • Video Tutorials
  • All HTML Tags
  • Browser Kits
  • Statistics

    Total online: 1
    Guests: 1
    Users: 0
    Creative Commons License
    Free Downloads and Services by MiX1 is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License
    Copyright MyCorp © 2018