Skip to main content

Download File from Remote Server with PHP CURL

On my previous post I was shown how we can get a remote file size without download it in php. Today I am going to show you how can we download a file from remote server with php.

There are many ways in PHP to download file from remote server. We can use php functions like copy, file_get_content, fopen, fsockopen to download remote files. These functions are well but today we will use curl to download file. Because it the advanced way to working with remote resources.We can download large file with minimum memory usages in this way.

getHeaders Function

This function gets all header information before downloading a file. This function will help you to know file is exist or not and filesize

 * Get Headers function
 * @param str #url
 * @return array
function getHeaders($url)
  $ch = curl_init($url);
  curl_setopt( $ch, CURLOPT_NOBODY, true );
  curl_setopt( $ch, CURLOPT_RETURNTRANSFER, false );
  curl_setopt( $ch, CURLOPT_HEADER, false );
  curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
  curl_setopt( $ch, CURLOPT_MAXREDIRS, 3 );
  curl_exec( $ch );
  $headers = curl_getinfo( $ch );
  curl_close( $ch );

  return $headers;

download Function

This function download file form remote server.

 * Download
 * @param str $url, $path
 * @return bool || void
function download($url, $path)
  # open file to write
  $fp = fopen ($path, 'w+');
  # start curl
  $ch = curl_init();
  curl_setopt( $ch, CURLOPT_URL, $url );
  # set return transfer to false
  curl_setopt( $ch, CURLOPT_RETURNTRANSFER, false );
  curl_setopt( $ch, CURLOPT_BINARYTRANSFER, true );
  curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
  # increase timeout to download big file
  curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 10 );
  # write data to local file
  curl_setopt( $ch, CURLOPT_FILE, $fp );
  # execute curl
  curl_exec( $ch );
  # close curl
  curl_close( $ch );
  # close local file
  fclose( $fp );

  if (filesize($path) > 0) return true;


$url = 'http://path/to/remote/file.jpg';
$path = 'uploads/file.jpg';

$headers = getHeaders($url);

if ($headers['http_code'] === 200 and $headers['download_content_length'] < 1024*1024) {
  if (download($url, $path)){
    echo 'Download complete!'; 


  1. I'm getting a 403 return, what to do?

  2. If I want to copy multiple files at the same time..

  3. Thank for this nice and functional code!

  4. how to download a file of 1.5 gb in size directly to server. it says after some 100 mb connection timeout.

  5. Really nice code !
    I have one question : if you want to download from HTTPs, how could you do ?
    When I put curl_setopt($ch, CURLOPT_SSLVERSION,3); I have
    SSL certificate problem: unable to get local issuer certificate


  6. Hello Admin, thank you for enlightening us with your knowledge sharing. PHP has become an inevitable part of web development, and with proper PHP training in Chennai, one can have a strong career in the web development field. We from Fita provide PHP course in Chennai with the best facilitation. Any aspiring students can join us for the best PHP training institute in Chennai.

  7. Perfecto. Excelente aporte. No habia tenido la oportunidad de usar curl y me funciono excelente a la primera trabajarlo para varias imagenes en DOM. Gracias.

  8. I have read your blog its very attractive and impressive. I like it your blog.

    PHP Training in chennai | PHP Training Course

    PHP Training in chennai | Online PHP Course

  9. That all I need for my download script by using PHP. Thanks for your tut!
    You can visit in free time to relax :)

  10. it get error: Fatal error: Call to undefined function getHeaders() in xml/wget.php on line 6

  11. This comment has been removed by the author.

  12. thanks for share

  13. Great blog created by you. I read your blog, its best and useful information. You have done a great work. Super blogging and keep it up.php jobs in hyderabad.

  14. Thank you very much, this worked well when fopen was not working for me.

  15. Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here. php training in chennai

  16. It is a very nice article including a lot of viral content. I am going to share it on social media. Get the Cold Pressed Oil

  17. Excellent post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
    Best web Designing Training Training Institute in Kanchipuram

  18. Drinking water from a copper water bottle has significant health benefits. Water stored in a copper vessel (which is known as “tamra jal” in Ayurvedic medicine.

    In the ancient days, it was a trend to store water in water bottle copper vessels and the water would remain healthy for drinking even after days.

    coconut oil doesn’t turn into fat. This is because cold press coconut oil contains medium-chain triglycerides (MCT), a body fuel that’s easy to burn without transforming to fat.

  19. The human body cannot synthesize on its own. copper bottle online is essential for certain metabolic functions.

    In the ancient days, it was a trend to store water in online copper water bottle vessels and the water would remain healthy for drinking even after days.

    One of the most important things to keep in mind is - that cold press oil behaves differently when heated, it changes texture, color, taste as well as it's nutritional properties.

    copper jug more and more people are becoming health conscious, they are shifting to healthier living habits including healthier cooking and eating habits too.

  20. copper glass has many medicinal abilities. Perhaps the most understated is its role in the prevention of and the fight against anemia.

    The human body cannot synthesize on its own. copper water bottle online bottle is essential for certain metabolic functions.

    jaggery palm is mainly used to prepare sakkarai pongal and for general home use.

    nattu sakaraiNattu Sakarai is organic and it is naturally produced by boiling sugar cane juice in a particular consistency.

  21. Thanks for the post. It was very interesting and meaningful. I really appreciate it! Keep updating stuff like this.
    Data Science
    ETL Testing
    Python Online Classes


Post a Comment

Popular posts from this blog

Multiple File Upload with Progress Bar using PHP & jQuery

Some days age I have created a post that deals with How we can upload file with progress bar using php and jQuery . Some of my readers asked me how we can upload multiple files with progress bar. In this post I am going to show you how we can upload multiple files with progress bar. We can upload multiple files with progress bar exactly same way as I shown previous. But we need to do some modification with our file upload form and our php script to handle multiple files.

Multiple File Upload with PHP

Sometimes we need to allow our users to upload multiple file upload. On my previous post I was shown the basic of simple file upload with PHP. Today I am going to show you how to allow users to upload multiple files. It is almost similar like simple file upload but we need to do some modification with html markup and php code . multiple attribute is needed to add on the html markup. Major web browsers like Firefox, Chrome, Safari and IE 9+ support this attribute. Now I am going to show you how to upload multiple file with PHP and basic HTML form.

File Upload with Progress Bar using jQuery and PHP

In this post you will learn how to create a AJAX file uploading system with progress bar using jQuery and PHP. For this purpose we will going to use jQuery Form Plugin . It is a easy to use and powerful jQuery AJAX form submitting plugin. It supports XMLHttpRequest Level 2 and iframe file transportation. That's why it will work for both old and new browsers.