PHP download file from url by using curl example

Share Me
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Today now in this post i will show you how to download file from url by using curl. Whenever we have need to download any file or image from URL by using php curl. Then we can see that example. we can download image or aby file from given url and we can save in over local server. We can do that by using get_file_contents() in php too, but i think it is good if we are doing that by using PHP curl.

So let’s start and see following example :

Example

$url = 'http://www.codingspoint.com/398816893_13433370444.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);

Read Also : How to Create Custom Blade Directive in Laravel?

Thanks for read. I hope it help you. For more you can follow us on facebook.

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →