"php_connect_nonb() failed: Operation now in progress (115)" with TLS/SSL-encrypted FTP connection in PHP - Unencypted connection works

"php_connect_nonb() failed: Operation now in progress (115)" with TLS/SSL-encrypted FTP connection in PHP - Unencypted connection works

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

出于某种未知原因,当尝试连接并放置 PHP 启用加密的文件时,响应为:

Warning: 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." );
    }
}

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

Attempt to connect to xx.xx.xx.xx:21 with SSL+FTP Succeeded. Now logging in ...
FTP connection was successful with xx.xx.xx.xx:21
FTP connection was set to PASSIVE mode.

但是当系统点击这个时:

$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;

响应是错误

Warning: 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 地址而不是 public 引起的,但我已经用其他程序测试过并且所有运行正常, FTP 服务器在执行 PASV 命令时发送 public IP。

我也用 curl 测试过,我可以在不加密的情况下连接。但是当我使用 TLS 时,系统在发送 PASV 并从服务器 收到应答后立即断开连接“227 进入被动模式 (xxx.xxx.xxx.xxx.198.104)”。 curl 错误为 7,无法连接到主机...可能与服务器配置错误中的 TLS 相关?

有什么想法吗?

PHP code 和 curl 的相似行为表明防火墙正在阻止数据连接。

由于未加密的连接有效,防火墙很可能正在 "smart" 监视 FTP 控制连接并自动转发在控制连接中找到的数据连接端口。但这不适用于加密会话,因为防火墙无法监控它们。

另请参阅我的 guide about network set up for FTP 的 "Smart Firewalls/NATs" 部分。

如果您需要允许加密连接,您需要与服务器管理员联系。

我有同样的问题,通过将 ftp 服务器 ip 添加到防火墙允许 ip 列表

来解决
ftp_set_option($ftpconn, FTP_USEPASVADDRESS, false);

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