PHP cURL Download File From Url Example

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.

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.

  Amazon S3 File Upload in JavaScript Example

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 = 'http://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 :

  Laravel Datatables edit Column HTML Source Code render

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.

Leave a Reply

avatar
  Subscribe  
Notify of