26, October 2014

Webdigity tutorials : Download files with cURL

Webdigity webmaster forums
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Tutorials @ webDigity
Download files with cURL

Subscribe to our tutorials feed!

A snippet that helps you download files from the web, with the use of the well known cURL library for php.

In this tutorial we will try to fetch a file from the web, using the cURL library.

As you may know the cURL library provides us with a fast and easy way to fetch content from the web, as in adition to the well known socket functions - which are used by the cURL library - cURL will also follow headers, send headers, and in general it will work more like a browser.

In this cURL tutorial, we've learned how to fetch content from a web page (eg. an RSS feed) and it can be also used to fetch files, but with a big impact in the memory, as this implementation will fetch the whole file to the memory and then we will be able to save it to the disk.

To prevent this, cURL has an embedded directive called CURLOPT_FILE which gets a file handler as a parameter, which is using for saving the file while it is downloaded. Here is how we use this directive :


where $fp is our file handler.

Here is a complete snippet which will download and save files for you. Take a look at the comments for some additional information :

This is usefull when you are downloading big files, as it
will prevent time out of the script :
ini_set('display_errors',true);//Just in case we get some errors, let us know....

$fp fopen (dirname(__FILE__) . '/a.rss''w+');//This is the file where we save the information
$ch curl_init('http://www.webdigity.com/rss.php');//Here is the file we are downloading

That's all. After this script is finished, we should have a file called a.rss to the script's folder.

Enjoy ;)
Do you like this tutorial?
Feel free to share it with others using Dzone or your favorite bookmarking service. You can see related discussions in our php forum
This tutorial posted by Nikolas from Sublime Directory

Sponsors :

Code library menu :
[ Tutorials home | Submit a tutorial ]

User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
Oct 26, 2014, 10:29:11 am

Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!

Forum Statistics
Total Posts: 81.362
Total Topics: 14.447
Total Members: 30.298
Tutorials : 58
Resources : 929
Designs : 395
Latest Member: banerbellacasa

288 Guests, 3 Users online :

8 users online today:

Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2014 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.