简体   繁体   English

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

[英]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 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. 当在浏览器中命中时,此URL强制下载文件,但我想使用CURL在本地路径上下载此文件。

Is there any way, thanks for help 有什么办法,谢谢帮忙

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 参考: 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. wget也可以帮助您摆脱这种麻烦。 input: 输入:

wget your_url

curl URL >file_path 卷曲URL> file_path

or u can use flags -o (lowercase o) or -O (uppercase o) 或u可以使用标志-o(小写o)或-O(大写o)

curl -o URL file_path curl -o URL file_path

the above command will save the output in the mentioned path 上面的命令会将输出保存在上述路径中

curl -O URL curl -O URL

the above command will take the filename name from the URL and store the result with that name 上面的命令将从URL中获取文件名,并以该名称存储结果

Example: 例:

curl -O www.xyz.com/search/clothes.html 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 将创建一个名为clothes.html的新文件,并将输出存储在该文件中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM