dongweizhen2009 2011-04-09 19:30
浏览 86
已采纳

使用PHP通过FTP实时上传文件

Is it possible to upload a file to the FTP server using PHP script while the same script is downloading the meant file from somewhere else? So at the time script is downloading it should upload the file in real-time.

  • 写回答

3条回答 默认 最新

  • duanfang7757 2011-04-09 19:42
    关注

    Easy as cake, in theory. First, see FTP function here: http://www.php.net/manual/en/function.ftp-fput.php. Then we use FOPEN wrappers ( http://www.php.net/manual/en/wrappers.php ) to open the file we want to read, and send it over.

    To modify the php.net example:

    <?php
    
    // open some file for reading
    $file = 'somefile.txt';
    $fp = fopen('ftp://user:pass@domain.com/' . $file, 'r');
    
    // set up basic connection
    $conn_id = ftp_connect($ftp_server);
    
    // login with username and password
    $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
    
    // try to upload $file
    if (ftp_fput($conn_id, $file, $fp, FTP_ASCII)) {
        echo "Successfully uploaded $file
    ";
    } else {
        echo "There was a problem while uploading $file
    ";
    }
    
    // close the connection and the file handler
    ftp_close($conn_id);
    fclose($fp);
    
    ?>
    

    Oh, and you might want non-blocking sometimes: http://www.php.net/manual/en/function.ftp-nb-fput.php

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

报告相同问题?

悬赏问题

  • ¥15 我这模型写的不对吗?为什么lingo解出来的下面影子价格这一溜少一个变量
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波