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 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作