如何使用CURL将文件下载并保存到本地路径?

我有个链接:www.domain.com/file.asp?File=peoples.csv

当在浏览器中点击时,这个URL会强制下载文件,但是我想使用curl在我的本地路径上下载这个文件。

有什么办法吗,谢谢你的帮助!

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

May info below helps you.

curl your_url >your_file_path

and wget may also helps you slove this porblem. input:

wget your_url

curl URL >file_path

or u can use flags -o (lowercase o) or -O (uppercase o)

curl -o URL file_path

the above command will save the output in the mentioned path

curl -O URL

the above command will take the filename name from the URL and store the result with that name

Example:

curl -O www.xyz.com/search/clothes.html

A new file with the name clothes.html will be created and the output will be stored in that file