“php_connect_nonb() failed: Operation now in progress (115)”,在 PHP 中使用 TLS/SSL 加密的 FTP 连接 - 未加密的连接有效

Dan*_*yra 3 php ftp passive-mode

我正在尝试为我的服务器的 cPanel 帐户使用自动备份脚本。
我已经在一台带有 FileZilla 服务器的本地机器上安装了一个 FTP 服务器。我可以使用 FileZilla 客户端、Plesk 服务器和 lftp 成功连接和传输文件...
服务器只接受加密连接并启用被动模式。

由于某些未知原因,当尝试使用 PHP 连接和放置文件时,响应是:

警告:ftp_put(): php_connect_nonb() failed: Operation now in progress (115) in
/home/xxxxxxx/public_html/lab/test/perform_mysql_bk.php on line 326

对这个错误有什么想法吗?

对于我正在使用的连接:

$fSuccess = false;

$sServerAddress = $this->m_aConfig['FTP_SERVER_ADDRESS'];
$iServerPort = (int)( $this->m_aConfig['FTP_SERVER_PORT'] );

// set up FTP connection
if ( ($this->m_aConfig['FTP_USE_SSL'] == 'YES') ) {

    if ( function_exists('ftp_ssl_connect') ) {
        $this->m_oFtpConnection = ftp_ssl_connect( $sServerAddress, $iServerPort );

        if ( !$this->m_oFtpConnection ) {
            $this->writeLog( "Attempt to connect to ".$sServerAddress.":".$iServerPort." with SSL+FTP failed. Will fallback to normal FTP." );
        }
        else {
            $this->writeLog( "Attempt to connect to ".$sServerAddress.":".$iServerPort." with SSL+FTP Succeeded. Now logging in ..." );
        }
    }
    else {
        $this->writeLog( "This server doesn't support FTPS (FTP with SSL). Will fallback to normal FTP." );
    }
}

//Fallback
if ( !$this->m_oFtpConnection ) {
    $this->m_oFtpConnection = ftp_connect( $sServerAddress, $iServerPort );
}

// login after a successful connection
if ( $this->m_oFtpConnection ) {
    $fLoginResult = ftp_login( $this->m_oFtpConnection, $this->m_aConfig['FTP_USERNAME'], $this->m_aConfig['FTP_PASSWORD'] ); 
    //echo $fLoginResult;
}
else {
    $this->writeLog( "Attempt to connect to ".$sServerAddress.":".$iServerPort." failed." );
}

// check connection
if ( (!$this->m_oFtpConnection) || (!$fLoginResult) ) { 
    $this->writeLog( "FTP connection has failed!" );
} else {
    $this->writeLog( "FTP connection was successful with ".$sServerAddress.":".$iServerPort );
    $fSuccess = true;
}

// Set to Passive connection if login was successful and this setting was set.
if ( $fSuccess && ($this->m_aConfig['FTP_USE_PASSIVE'] == 'YES') ) {

    if ( ftp_pasv( $this->m_oFtpConnection, true ) ) {
        $this->writeLog( "FTP connection was set to PASSIVE mode." );
    }
    else {
        $this->writeLog( "Attempted to set FTP connection to PASSIVE mode but failed. Going to continue with copy anyway. If the script fails, review this as a possible source." );
    }
}
Run Code Online (Sandbox Code Playgroud)

响应成功:(我已经用xx值替换了我的IP)

尝试使用 SSL+FTP 连接到 xx.xx.xx.xx:21 成功。现在登录...
FTP 连接成功, xx.xx.xx.xx:21
FTP 连接设置为被动模式。

但是当系统遇到这个时:

$fSuccess = false;

$sDestinationFile = $this->m_aConfig['FTP_PATH_TO_COPY'] . $insDbFileName;

// upload the file
$fUpload = ftp_put( $this->m_oFtpConnection, $sDestinationFile, $insDbFileName, FTP_BINARY ); 

// check upload status
if (!$fUpload) { 
    $this->writeLog( "FTP upload has failed! Check the log file for errors. Try changing PASSIVE and SSL options in the config." );
    return false;
} else {
    $this->writeLog( "Uploaded $insDbFileName to ".$this->m_aConfig['FTP_SERVER_ADDRESS']." as $sDestinationFile" );
    $fSuccess = true;
}

return $fSuccess;
Run Code Online (Sandbox Code Playgroud)

响应是错误

警告:ftp_put(): php_connect_nonb() failed: Operation now in progress (115) in
/home/xxxxxxx/public_html/lab/test/perform_mysql_bk.php on line 326

我读过很多页面说这个错误是由 FTP 服务器返回一个内部 IP 地址而不是公共 IP 地址引起的,但我已经用其他程序测试过,一切运行正常,执行 PASV 命令时 FTP 服务器正在发送公共 IP .

有任何想法吗?

小智 6

ftp_set_option($ftpconn, FTP_USEPASVADDRESS, false);
Run Code Online (Sandbox Code Playgroud)

这行代码在设置连接的被动之前 ftp_pasv($ftpconn, true); 救了我的命。