drvpv7995
2013-07-24 07:01
采纳率: 100%
浏览 3.3k
已采纳

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

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

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

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

图片转代码服务由CSDN问答提供 功能建议

我的URL如下所示

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

此URL强制在浏览器中命中时下载文件,但我想使用CURL在本地路径下载此文件。 \ n

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • duanchi3109 2013-08-16 10:05
    已采纳

    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

    点赞 打赏 评论
  • douchi0638 2013-07-24 07:11

    May info below helps you.

    curl your_url >your_file_path
    

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

    wget your_url
    
    点赞 打赏 评论
  • doumeng1089 2016-07-12 06:18

    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

    点赞 打赏 评论

相关推荐 更多相似问题