Download files using Curl

Download remote files programatically using curl.
A simple and effective function to download files hosted on remote server. In the following function set the variable $dir to your directory in which you want to store downloaded files.

Example given below how to call this function. Just pass the url of the file to be downloaded and a filename. The file will be downloaded with this filename in the given directory.

  function download_file_curl($url,$name){ 
   $fp = fopen ($dir . $name, 'w+');
   $ch = curl_init($url);
   curl_setopt($ch, CURLOPT_TIMEOUT, 30);
   curl_setopt($ch, CURLOPT_FILE, $fp);
   curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
   $data = curl_exec($ch);
   fwrite($fp, $data);

Call this function as follows


$url is the file to be downloaded.
The file will be named $name after downloading.
Download files using Curl Download files using Curl Reviewed by Altaf Hussain on February 27, 2013 Rating: 5


  1. image on your blog background is not reachable, which makes hard to read text.

  2. There was a problem with the theme. I have updated it and its ok now.


Altaf Web. Powered by Blogger.