Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download and save a file to local path using CURL

I have a URL as following

www.domain.com/file.asp?File=peoples.csv

This URL force downloads the file when hit in browser but i want to download this file on my local path using CURL.

Is there any way, thanks for help

like image 632
kasim badami Avatar asked Dec 25 '22 23:12

kasim badami


1 Answers

Ok, got the solution. Sharing my answer.

$getFile = 'http://url to file/file.csv';
$getParams  = array ();

$path = '/var/save/to/local/path/file_name.csv';
$fp = fopen($path, 'w');
$ch = curl_init($getFile);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies');
$data = curl_exec($ch);

if(fwrite($fp,$data))
{
    return true;
}
else
{
    return false;
}

Thanks all for your help. Reference : http://www.technologyworkshops.net/php/how-to-download-and-save-a-file-to-local-path-using-curl-t132.html

like image 181
kasim badami Avatar answered Dec 29 '22 10:12

kasim badami