4

我有一个如下网址

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

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

有什么办法吗,谢谢帮助

4

3 回答 3

5

好的,得到了​​解决方案。分享我的答案。

$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;
}

感谢你的帮助。参考: http ://www.technologyworkshops.net/php/how-to-download-and-save-a-file-to-local-path-using-curl-t132.html

于 2013-08-16T10:05:30.397 回答
3

卷曲 URL >file_path

或者你可以使用标志 -o(小写 o)或 -O(大写 o)

curl -o URL 文件路径

上面的命令会将输出保存在提到的路径中

curl -O 网址

上面的命令将从 URL 中获取文件名并使用该名称存储结果

例子:

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

将创建一个名为 dress.html 的新文件,输出将存储在该文件中

于 2016-07-12T06:18:06.040 回答
2

愿以下信息对您有所帮助。

curl your_url >your_file_path

wget 也可以帮助你解决这个问题。输入:

wget your_url
于 2013-07-24T07:11:27.483 回答