How To 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!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes CURL Download Useful Bits & Links
Tutorial Video The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

QUICK NOTES

If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

 

PHP CURL FILE DOWNLOAD

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

 

EXAMPLE 1) USING CURL TO DOWNLOAD FILES

1-curl-download.php
<?php
// (A) SETTINGS
set_time_limit(0); // No script timeout, if expecting large download
$source = "http://localhost/README.txt"; // Target file to download
$destination = "saved.txt"; // Save to this file
$timeout = 30; // 30 seconds CURL timeout, increase if downloading large file

// (B) FILE HANDLER
$fh = fopen($destination, "w") or die("ERROR opening " . $destination);

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

// (D) CURL RUN
curl_exec($ch);
if (curl_errno($ch)) {
  // (D1) CURL ERROR
  echo "CURL ERROR - " . curl_error($ch);
} else {
  // (D2) CURL OK
  // NOTE: HTTP STATUS CODE 200 = OK
  // BAD DOWNLOAD IF SERVER RETURNS 401 (UNAUTHORIZED), 403 (FORBIDDEN), 404 (NOT FOUND)
  $status = curl_getinfo($ch);
  // print_r($status);
  echo $status["http_code"] == 200 ? "OK" : "ERROR - " . $status["http_code"] ;
}

// (D3) 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 a file on the local server.
  3. The CURL request itself.
  4. Error handling and what to do after the download.

 

 

EXAMPLE 2) DOWNLOADING LARGE FILES

2-large-download.php
<?php
// (A) SETTINGS
set_time_limit(0);
$source = "http://localhost/README.txt";
$destination = "saved.txt";
$block = 4096; // Read 4096 bytes per block to prevent memory

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

// (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 real simple file download… We don’t actually even need CURL. Just file_put_contents() and file_get_contents().

 

 

USEFUL BITS & LINKS

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

 

TUTORIAL VIDEO

 

INFOGRAPHIC CHEAT SHEET

PHP CURL File Download (click to enlarge)

 

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 *