PHP CURL File Download – A Simple Example

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.

 

 

REAL QUICK TUTORIAL

 

TABLE OF CONTENTS

Download & Notes CURL Download Useful Bits & Links
The End

 

DOWNLOAD & NOTES

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

 

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.

 

QUICK NOTES

If you spot a bug, please feel free to comment below. I try to answer 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.

 

 

PHP CURL FILE DOWNLOAD

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

 

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.

  • A – A bunch of settings, where to download the file from, where to save it to.
  • B – Creating a file on the local server.
  • C – The CURL request itself.
  • D – Error handling and what to do after the download.

 

 

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 $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.

 

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 main tutorial, and here is a small section on some extras and links that may be useful to you.

 

INFOGRAPHIC CHEAT SHEET

PHP CURL File Download (click to enlarge)

 

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 *