Hi! In this post, we will see how to download file from url using php curl. CURL is a great tool when it comes to remote communication. Using it, you can easily connect to a remote server and download files to your local machine. It allows to send http post request and get request in php as well.
Executing a basic curl request will simply return the data to the output stream. But we don't want that. Instead, we must assign it to a php variable, which we can write it to the disk. Using CURLOPT_RETURNTRANSFER is the easiest way to copy remote files to our own. But there is a problem with this method. Below we will see the right way to download remote files with curl.
PHP - Download File from URL using cURL:
To download files from the remote url with curl, you have to follow the below steps:
- Create a writable file stream
- Pass the file handle to curl
- This will make cURL write the content downloaded directly into the file.
- Close the file handle.
As I said earlier, using CURLOPT_RETURNTRANSFER
will pose a problem when we download huge files. You'll easily exceed memory limits, given the entire data has to be read into the memory before writing it to disk. Even if you increase the limit, it's an unnecessary load on the server.
Simply passing the writable file stream to the curl will make it copy the contents of the file directly. For this we have to use the CURLOPT_FILE
option.
Here is the code snippet to download files from the remote url.
<?php
$file_url = 'http://www.test.com/images/avatar.png';
$destination_path = "downloads/avatar.png";
$fp = fopen($destination_path, "w+");
$ch = curl_init($file_url);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_exec($ch);
$st_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
fclose($fp);
if($st_code == 200)
echo 'File downloaded successfully!';
else
echo 'Error downloading file!';
?>
This code will download the file contents from the given $file_url
and copy it to the $destination_path
. Make sure the destination folder has write permission so that the file is downloaded correctly.
Brief explanation of the functions we have used above,
- fopen() - The function opens a file or url and returns the file pointer.
- curl_init() - Initialize the curl session.
- curl_exec() - It executes the given curl session.
- curl_close() - Destroy an existing curl session.
- CURLOPT_FILE - The option instructs curl to save the data returned to the given file.
- curl_getinfo() - Returns the status details of the latest transfer.
For HTTPS Transfer:
If your file url contains 'https' instead of 'http', it is better to configure the SSL option in curl with this line,
Read Also:
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
- How to Upload, View & Download File in PHP and MySQL
- How to Connect to Remote MySQL Database using PHP
That explains about downloading file from url using php curl. With this method, you can download even large files from remote servers without running out of memory usage. Also make sure to include proper error handling in the production environment. I hope this tutorial is useful. Please share it on social media if you like it.