PHP FTP函数

PHP ftp_nb_continue() 函数继续以非阻塞方式检索/发送文件到 FTP 服务器。

语法

ftp_nb_continue(ftp) 

参数

ftp必需。 指定要使用的 FTP 连接。

返回值

返回以下任意值:

  • FTP_FAILED:异步传输失败。
  • FTP_FINISHED:异步传输已完成。
  • FTP_MOREDATA:异步传输仍处于活动状态。

示例:

下面的示例显示ftp_nb_continue()的用法功能。

<?php
//要使用的FTP服务器
$ftp_server = "ftp.example.com";
  
//FTP 连接的用户名
$ftp_user = "user";
  
//用户密码
$ftp_pass = "password";
  
//建立连接或者连接失败
$ftp = ftp_connect($ftp_server)
    or die("Could not connect to $ftp_server");
   
if($ftp) {
  echo "Successfully connected to $ftp_server!\n";
 
  //尝试登录
  if(@ftp_login($ftp, $ftp_user, $ftp_pass)) {
    echo "Connected as $ftp_user@$ftp_server\n";
      
    //启动下载
    $ret = ftp_nb_continue($ftp, "local_demo.txt", 
                    "server_demo.txt", FTP_ASCII);

    while ($ret == FTP_MOREDATA) {
      //继续下载...
      $ret = ftp_nb_continue($ftp);
    }

    if ($ret == FTP_FINISHED) {
      echo "Successfully downloaded the file\n";
    } else {
      echo "Error while downloading the file\n";
      exit(1);
    }
    
  } else {
    echo "Couldn't connect as $ftp_user\n";
  }
 
  //关闭连接
  if(ftp_close($ftp)) {
    echo "Connection closed successfully!\n"; 
  } 
}
?> 

上述代码的输出将是:

Successfully connected to ftp.example.com!
Connected as user@ftp.example.com
Successfully downloaded the file
Connection closed successfully!