Support Ticket Why UserCV? [FAQ] Knowledgebase Personal Blogging Platform Start Teaching Online
Downloading a "zip" or "pdf" or "doc" or "ppt" or "Image" file using PHP cURL

In below tutorial, we will learn how to download and save file using PHP cURL. 

PHP cURL is a library that lets you make HTTP requests in PHP. Everything you need to know about it (and most other extensions) can be found in the PHP manual.

You can make HTTP requests without cURL, too, though it requires allow_url_fopen to be enabled in your php.ini file.

We will see the alternate code sample option later but first let's get to the cURL code:

 

Warning, Danger:

curl is evil and dangerous if used improperly because it is all about getting data from out there in the internet. Someone can get between your curl and the other server and inject a rm -rf / into your response, and then why am I dropped to a console and ls -l doesn't even work anymore? Because you mis underestimated the dangerous power of curl. Don't trust anything that comes back from curl to be safe, even if you are talking to your own servers. You could be pulling back malware to relieve fools of their wealth.

 

Before  you see the code, you must know:

- PHP programming language

- cURL basics

 

Let's see the Code now:

In below code, we will be downloading "wordpress latest" file from wordpress official website:

<?php

$fileToDownload = "https://wordpress.org/latest.zip";

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $fileToDownload);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0');
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_VERBOSE,true);
$data = curl_exec ($ch);
$error = curl_error($ch); 
curl_close ($ch);

if ($error) {
    die(var_dump($error));
}

$destination = "./wordpress.zip";
$file = fopen($destination, "w+");
fputs($file, $data);
fclose($file);

echo "File has been downloaded @ ".$destination;

?>

In above code,

- variable $fileToDownload contains the link to download file.

- variable $destination contains the path and name where link file will be download, written and saved.

 

Now, I promised to share alternative method as well, Well here it is:

 

<?php

$fileToDownload = "https://wordpress.org/latest.zip";

$downloadedFileContents = file_get_contents($url);
 

if($downloadedFileContents === false){
    die(var_dump("Failed to download file at: " . $url));
}
 
$fileName = "wordpress.zip";

$save = file_put_contents($fileName, $downloadedFileContents);
 
if($save === false){
    die(var_dump('Failed to save file to: ' , $fileName));
}

echo "File has been downloaded";

 

Hope above code helps, Any question? ask them in below comments.

Enjoy!


Tags: php. cURL file_get_contents file_put_contents php download download file using cURL download file using file_get_contents


acebook
About the Author
Comments