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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀