PHP Download File from URL using CURL Request Example

By Hardik Savani November 5, 2023 Category : PHP CURL

Hi Folks,

In this profound tutorial, we will learn php curl download image from url. let’s discuss about php download file from url using curl. if you want to see an example of php save file from url to server then you are in the right place. I explained simply about php save file from url curl. follow the below example for php curl save file to disk.

Whenever you require to download a file or image from a URL using php curl. then you can see that example. we can download images or files from the given URL and save them on over 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. let's see the following example :

Example:

index.php

<?php

$url = 'http://www.test.com/1458398816_33370444.jpg';

$curlCh = curl_init();

curl_setopt($curlCh, CURLOPT_URL, $url);

curl_setopt($curlCh, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($curlCh, CURLOPT_SSLVERSION,3);

$curlData = curl_exec ($curlCh);

curl_close ($curlCh);

$downloadPath = "upload/flower10.jpg";

$file = fopen($downloadPath, "w+");

fputs($file, $curlData);

fclose($file);

?>

I hope it can help you...

Tags :
Shares