PHP cURL Download File From Url Example

Today, We want to share with you PHP cURL Download File From Url Example.In this post we will show you Downloading files with cURL in PHP, hear for PHP cURL for Downloading File From Url we will give you demo and example for implement.In this post, we will learn about php download file from url using curl with an example.

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

PHP cURL Download File From Url Example

There are the Following The simple About PHP cURL Download File From Url Example Full Information With Example and source code.

As I will cover this Post with live Working example to develop php save file from url to server, so the php save file from url curl for this example is following below.

Example 1: Php Serever Side Curl download file/Images

$url = 'https://www.pakainfo.com/free_download_pakainfo.jpg';

//PHP download file from url using curl example
$curlCh = curl_init();
curl_setopt($curlCh, CURLOPT_URL, $url);
curl_setopt($curlCh, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlCh, CURLOPT_SSLVERSION,3);
$curlrequestData = curl_exec ($curlCh);
curl_close ($curlCh);


$download_live_Path = "media/pakainfo.jpg";
$file = fopen($download_live_Path, "w+");
fputs($file, $curlrequestData);
fclose($file);

Example 2: Downloading files with cURL in PHP

<?php

//The resource that we want to download.
$fileUrl = 'https://www.pakainfo.com/img/logo.png';

$filesaveTo = 'logo.png';

$fp = fopen($filesaveTo, 'w+');

if($fp === false){
    throw new Exception('Could not open: ' . $filesaveTo);
}

$ch = curl_init($fileUrl);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_exec($ch);

if(curl_errno($ch)){
    throw new Exception(curl_error($ch));
}

$getstatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
fclose($fp);
if($getstatusCode == 200){
    echo 'Downloaded!';
} else{
    echo "Status Code: " . $getstatusCode;
}

Angular 6 CRUD Operations Application Tutorials

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about PHP cURL Download File From Url Example.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1.   Vue.js Alert popup-Dialog Box using PHP and CSS
  2.   Laravel 6 Auth Redirection Using redirectTo
  3.   Laravel 7 jQuery Ajax Form Validation Example
  4.   How to display cross domain json data using Jquery
  5.   Vue component render function using vuejs
  6.   Search Filter using Nodejs and MySQL
  7.   PHP download file from url using curl example
  8.   JQuery Set Focus on First TextBox
  9.   WordPress Plugin Redirect After Activation
  10.   React Colorpicker Event change

Leave a Reply

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