PHP download file from url using curl example

In this article, I am going to explain “How to download file from url using curl in php”. Whenever you require to download file or image from URL using php CURL then you can refer this example. We can download image or file from URL and save in any specific location where want to store like local server. You can do that using get_file_contents() in php too, but i think it is good if you are doing that using PHP curl. But before use cURL you need to know what is cURL and how to use in php

What is cURL

CURL- Client URL Library
CURL connects different types of servers and communicates with different types of protocols like http, https and ftp.

In order to use CURL, you need to install the libcurl package.



You can check whether it is installed or not using the phpinfo function. For this you can create a test php page like test.php

The following code will be included in that page and run on your server:

If CURL is installed, you will see the following:

If CURL package is enabled, then no need to install and you can directly use CURL function. Otherwise you need to install it or you can contact your hosting provider to install CURL package.

curl_init() function is used to start the CURL session. After initializing the CURL session, you have to define the CURL options via curl_setopt() function.
After defining the options, curl_exec() function is executed and it returns the response (TRUE on success or FALSE on failure).
curl_close($ch) function is used to close the CURL session.

Example:-

Now, you can refer the given example for download file from URL using cURL in php example is given below:

 

I hope you might have understood it properly, keep reading our other blogs posts for more coding tricks.

Thanks!!

Add a Comment

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