banner I am Dev. A Freelance Web Developer from India. I have been working on for 2 years on PHP/MySQL, AJAX and related coding.

And personally I almost like everything that is OpenSource. I usally experiement with different opensource projects like RubyOnRails, OpenSocial, Facebook API and many more. More...
Sep
19th

PHP Function To Download Remote File

Author: Dev | Files under PHP

As I promised in my first post. I am starting with a simple PHP function that have saved a lot of time in uploading files to the server.

There are two ways in doing this depending on your server configuration. The first one uses PHP cURL module while the second uses built-in PHP function file_get_contents. I always use and prefer using cURL as it workes like a charm. Also, the problem with file_get_contents is that it is normally blocked by most of the server easily.

1) Remote file download using cURL

PHP:
  1. // PHP Funtion : curl_download uses PHP curl module to download the file.
  2. // It takes two parameter 1) Remote file url and 2) Local file name
  3. function curl_download($url,$out_file_name){
  4. ini_set('memory_limit', '1000M');
  5.  
  6. $out = fopen($out_file_name,"wb");
  7.  
  8. if($out){
  9. $ch = curl_init();
  10.  
  11. curl_setopt($ch, CURLOPT_FILE, $out);
  12. curl_setopt($ch, CURLOPT_HEADER, 0);
  13. curl_setopt($ch, CURLOPT_URL, $url);
  14.  
  15. curl_exec($ch);
  16. if(curl_error ($ch)){
  17. Error in downloading file. Curl error says : ".curl_error ($ch);
  18. }
  19. else{
  20. File Download Success.";
  21. }
  22.  
  23. curl_close($ch);
  24. }else{
  25. echo "Error : Set Permissions 777 to the current directory";
  26. }
  27. fclose($out);
  28. }

For example, if you want to download a wordpress zip file to your server, you can just call the following function:

PHP:
  1. normal_download("http://wordpress.org/latest.zip", "wordpress_latest.zip");

2) And Remote file download using file_get_modules. Use this only if you don't have cURL enabled.

PHP:
  1. // PHP Funtion: normal_download  uses file_get_contents PHP function to download the file.
  2. // It takes two parameter 1) Remote file url and 2) Local file name
  3. function normal_download($url,$out_file_name){
  4. ini_set('memory_limit', '1000M');
  5.  
  6. $out = fopen($out_file_name,"wb");
  7.  
  8. if($out){
  9.  
  10.  
  11. if(curl_error ($ch)){
  12. Error in downloading file. Curl error says : ".curl_error ($ch);
  13. }
  14. else{
  15. File Download Success.";
  16. }
  17.  
  18. }else{
  19. echo "Error : Set Permissions 777 to the current directory";
  20. }
  21.  
  22. fclose($out);
  23. }

For example, if you want to download a wordpress zip file to your server, you can just call the following function:

PHP:
  1. normal_download("http://wordpress.org/latest.zip", "wordpress_latest.zip");

Update: As Salil commented, if you are not sure which function to use then add this function along with the above two functions. This will which check it curl exists or not and uses it if it exists.

PHP:
  1. function auto_download($url,$out_file_name){
  2. if( function_exists("curl_init") )
  3. curl_download($url,$out_file_name);
  4. else
  5. normal_download($url,$out_file_name);
  6. }

Download code as zip file

( Downloaded times )

Like this post? Want to receive more like these!

Then, enter your email address here:

Delivered by FeedBurner


Do you like this article? then

2 responses. Wanna say something?

  1. Salil Jain
    Sep 22, 2008 at 06:37:33
    #1

    Best would be to combine the two where PHP checks if function (CURL or file_get_contents) is available or not.

  2. Dev
    Sep 22, 2008 at 12:38:04
    #2

    Salil: Yeah, Good Idea. Thanks. I will do that then.

Post a Comment