Download Files With CURL PHP (Simple Examples)

Welcome to a tutorial on how to download files with PHP CURL. Need to fetch a file from another server with PHP CURL? Yes, it is possible.

To download a file with PHP CURL, simply create a file handler with fopen() and pass it into the CURL options.

  • $fh = fopen("FILE", "w");
  • $ch = curl_init();
  • curl_setopt($ch, CURLOPT_URL, "HTTP://SITE.COM/FILE");
  • curl_setopt($ch, CURLOPT_FILE, $fh);
  • curl_exec($ch);
  • curl_close($ch);

That should cover the basics, but let us walk through a few more examples in this guide – Read on!

 

 

TABLE OF CONTENTS

 

DOWNLOAD & NOTES

Here is the download link to the example code, so you don’t have to copy-paste everything.

 

EXAMPLE CODE DOWNLOAD

Source code on GitHub Gist

Just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

SORRY FOR THE ADS...

But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.

Buy Me A Coffee Code Boxx eBooks

 

 

PHP CURL FILE DOWNLOAD

All right, let us now get into the examples of downloading files with PHP CURL.

 

TUTORIAL VIDEO

 

EXAMPLE 1) USING CURL TO DOWNLOAD FILES

1-curl-download.php
<?php
// (A) SETTINGS
set_time_limit(0); // no script timeout
$source = "http://localhost/README.txt"; // target file to download
$destination = "saved.txt"; // save to this file
$timeout = 30; // 30 seconds curl timeout

// (B) FILE HANDLER
$fh = fopen($destination, "w") or exit("Error opening $destination");

// (C) CURL FETCH & SAVE
// (C1) CURL INIT
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $source);
curl_setopt($ch, CURLOPT_FILE, $fh);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);

// (C2) CURL EXEC
// note: http status code 200 = ok
curl_exec($ch);
if (curl_errno($ch)) { echo curl_error($ch); }
else {
  $status = curl_getinfo($ch);
  // print_r($status);
  echo $status["http_code"] == 200 ? "OK" : "ERROR - " . $status["http_code"] ;
}

// (C3) CLOSE CURL & FILE
curl_close($ch);
fclose($fh);

Yes, this is practically just an “improved version” of the introduction snippet.

  1. A bunch of settings, where to download the file from, where to save it to.
  2. Creating an “empty file” on the local server.
  3. Use CURL to download and save the specified source.

 

 

EXAMPLE 2) DOWNLOADING LARGE FILES

2-large-download.php
<?php
// (A) SETTINGS
set_time_limit(0); // no script timeout
$source = "http://localhost/README.txt"; // target file to download
$destination = "saved.txt"; // save to this file
$block = 4096; // read 4096 bytes per block

// (B) FILE HANDLERS
$sh = fopen($source, "rb") or exit("ERROR opening $source");
$dh = fopen($destination, "w") or exit("ERROR opening $destination");

// (C) DOWNLOAD, BLOCK-BY-BLOCK
while (!feof($sh)) {
  if (fwrite($dh, fread($sh, $block)) === false) { echo "FWRITE ERROR."; }
  flush();
}

// (D) DONE!
echo "DONE.";
fclose($sh);
fclose($dh);

If you are dealing with very large files, I will recommend dropping CURL and just use fopen() fwrite() fread() instead – We can limit the number of bytes fread() can read at once, to prevent an “out-of-memory” error.

 

EXAMPLE 3) SUPER SIMPLE DOWNLOAD

3-simple-download.php
<?php
$source = "http://localhost/README.txt";
$destination = "saved.txt";
file_put_contents($destination, file_get_contents($source));
echo "OK";

For you guys who just want a really simple file download… We don’t actually even need CURL. Just file_put_contents() and file_get_contents().

 

 

EXTRAS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

LINKS & REFERENCES

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

Your email address will not be published. Required fields are marked *