doushuichong2589 2011-06-16 08:43
浏览 80
已采纳

PHP - 从URL下载并通过FTP上传

Slightly weird concept here... A client of ours wants data pushed to them over FTP/S

The idea is that we download one of our reports by downloading from a URL (a CSV File), then push this to the client over FTP/S. I know I can do this in bash scripts using wget & ftp - but need to add to this over a web interface so PHP is the best way forward.

As this is a background task I can extend time-outs etc.

I know also I can use fopen to download and save a file, then find it and upload it using the PHP FTP library. Just looking for a way to download using fopen, hold the data in memory to upload straight away.

Any help appreciated in advance!

  • 写回答

2条回答 默认 最新

  • doumiebiao6827 2011-06-16 09:01
    关注

    To retrieve the data from the URL you have a few options. You say you want the data in memory only to push directly to the FTP host.

    One approach (that I find the simplest to use, but lacking in terms of reliability and error handling) is file_get_contents()

    Example:

    $url = 'http://www.domain.com/csvfile';
    $data = file_get_contents($url);
    

    Now you have your csv data in $data, over to how to push this to an ftp server.

    Again the simplest way to do this is to use the builtin stream wrappers as used in the get example above. (Note however that this requires PHP 4.3.0)

    Simply build up the connection string like this.

    $protocol = 'ftps';
    $hostname = 'ftp.domain.com';
    $username = 'user';
    $password = 'password';
    $directory = '/pub';
    $filename = 'filename.csv';
    $connectionString = sprintf("%s://%s:%s@%s%s/%s",
        $protocol,$username,$hostname,
        $password,$directory,
        $filename);
    
    
    file_put_contents($connectionString,$data);
    

    Have a look at the ftp wrappers manual

    If this does not work there are other options.

    You could use curl to get the data and the FTP Extension to push it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)