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.
TLDR – QUICK SLIDES
Fullscreen Mode – Click Here
TABLE OF CONTENTS
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
<?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.
- A bunch of settings, where to download the file from, where to save it to.
- Creating an “empty file” on the local server.
- Use CURL to download and save the specified source.
EXAMPLE 2) DOWNLOADING LARGE FILES
<?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
<?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()
.
EXTRA 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
- CURL – PHP
- fopen – PHP
- How To Write & Append To Files In PHP – Code Boxx
TUTORIAL VIDEO
INFOGRAPHIC CHEAT SHEET

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!