Download and save a file using cURL in PHP

cURL is a computer software project providing a library and command-line tool for transferring data using various protocols. The cURL project produces two products, libcurl and cURL. It was first released in 1997. The name originally stood for "see URL".

Here is a code in PHP with cURL, that can download a remote file and save it.

<?php
set_time_limit(0);

$fp = fopen ('myfile.tar', 'w+');
 
$url = "http://www.example.com/file_name.tar";

$ch = curl_init(str_replace(" ","%20",$url));
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$data = curl_exec($ch);

curl_close($ch);
?>

The CURLOPT_FILE option takes a file resource and writes the content of the URL to that file resource/handle. Also set the script time limit to a large extent so that the script carries on while downloading a large file. That's all.
Emoticon? Emoticon

Komentar Terbaru

Just load it!