title: "php_connect_nonb() failed: Operation now in progress (115)" with TLS/SSL-encrypted FTP connection in PHP - Unencypted connection works
body: <p>I'm trying to use an automatic backup script for cPanel accounts of my server.<br />
  I have mounted an FTP server in one local machine with FileZilla Server.
  I can connect and transfer files successfully with FileZilla client, a Plesk server, and lftp...<br />
  The passive mode is enabled.</p>
  <p>For some unknown reason when trying to connect and put files with PHP with enabled encryption the response is:</p>
  <blockquote>
  <p>Warning: ftp_put(): php_connect_nonb() failed: Operation now in
  progress (115) in<br />
  /home/xxxxxxx/public_html/lab/test/perform_mysql_bk.php on line 326</p>
  </blockquote>
  <p>Any ideas for this error?</p>
  <p>Without encryption the upload works.</p>
  <p>For the connection I'm using:</p>
  <pre><code>$fSuccess = false;
  
  $sServerAddress = $this-&gt;m_aConfig['FTP_SERVER_ADDRESS'];
  $iServerPort = (int)( $this-&gt;m_aConfig['FTP_SERVER_PORT'] );
  
  // set up FTP connection
  if ( ($this-&gt;m_aConfig['FTP_USE_SSL'] == 'YES') ) {
  
      if ( function_exists('ftp_ssl_connect') ) {
          $this-&gt;m_oFtpConnection = ftp_ssl_connect( $sServerAddress, $iServerPort );
          
          if ( !$this-&gt;m_oFtpConnection ) {
              $this-&gt;writeLog( &quot;Attempt to connect to &quot;.$sServerAddress.&quot;:&quot;.$iServerPort.&quot; with SSL+FTP failed. Will fallback to normal FTP.&quot; );
          }
          else {
              $this-&gt;writeLog( &quot;Attempt to connect to &quot;.$sServerAddress.&quot;:&quot;.$iServerPort.&quot; with SSL+FTP Succeeded. Now logging in ...&quot; );
          }
      }
      else {
          $this-&gt;writeLog( &quot;This server doesn't support FTPS (FTP with SSL). Will fallback to normal FTP.&quot; );
      }
  }
  
  //Fallback
  if ( !$this-&gt;m_oFtpConnection ) {
      $this-&gt;m_oFtpConnection = ftp_connect( $sServerAddress, $iServerPort );
  }
  
  // login after a successful connection
  if ( $this-&gt;m_oFtpConnection ) {
      $fLoginResult = ftp_login( $this-&gt;m_oFtpConnection, $this-&gt;m_aConfig['FTP_USERNAME'], $this-&gt;m_aConfig['FTP_PASSWORD'] ); 
      //echo $fLoginResult;
  }
  else {
      $this-&gt;writeLog( &quot;Attempt to connect to &quot;.$sServerAddress.&quot;:&quot;.$iServerPort.&quot; failed.&quot; );
  }
  
  // check connection
  if ( (!$this-&gt;m_oFtpConnection) || (!$fLoginResult) ) { 
      $this-&gt;writeLog( &quot;FTP connection has failed!&quot; );
  } else {
      $this-&gt;writeLog( &quot;FTP connection was successful with &quot;.$sServerAddress.&quot;:&quot;.$iServerPort );
      $fSuccess = true;
  }
  
  // Set to Passive connection if login was successful and this setting was set.
  if ( $fSuccess &amp;&amp; ($this-&gt;m_aConfig['FTP_USE_PASSIVE'] == 'YES') ) {
      
      if ( ftp_pasv( $this-&gt;m_oFtpConnection, true ) ) {
          $this-&gt;writeLog( &quot;FTP connection was set to PASSIVE mode.&quot; );
      }
      else {
          $this-&gt;writeLog( &quot;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.&quot; );
      }
  }
  </code></pre>
  <p>The response is successful: (I have replaced my IP with xx values)</p>
  <blockquote>
  <p>Attempt to connect to xx.xx.xx.xx:21 with SSL+FTP Succeeded. Now
  logging in ...<br />
  FTP connection was successful with xx.xx.xx.xx:21<br />
  FTP connection was set to PASSIVE mode.</p>
  </blockquote>
  <p>But when the system hits this:</p>
  <pre><code>$fSuccess = false;
  
  $sDestinationFile = $this-&gt;m_aConfig['FTP_PATH_TO_COPY'] . $insDbFileName;
  
  // upload the file
  $fUpload = ftp_put( $this-&gt;m_oFtpConnection, $sDestinationFile, $insDbFileName, FTP_BINARY ); 
  
  // check upload status
  if (!$fUpload) { 
      $this-&gt;writeLog( &quot;FTP upload has failed! Check the log file for errors. Try changing PASSIVE and SSL options in the config.&quot; );
      return false;
  } else {
      $this-&gt;writeLog( &quot;Uploaded $insDbFileName to &quot;.$this-&gt;m_aConfig['FTP_SERVER_ADDRESS'].&quot; as $sDestinationFile&quot; );
      $fSuccess = true;
  }
  
  return $fSuccess;
  </code></pre>
  <p>The response is the error</p>
  <blockquote>
  <p>Warning: ftp_put(): php_connect_nonb() failed: Operation now in
  progress (115) in<br />
  /home/xxxxxxx/public_html/lab/test/perform_mysql_bk.php on line 326</p>
  </blockquote>
  <p>I have read many pages saying this error is caused by the FTP server returning an internal IP address instead of a public, but I have tested with other programs and all runs ok, the FTP server is sending the public IP when the PASV command is executed.</p>
  <p>I also tested with curl and I can connect without encryption. But when I use TLS, the system disconnects just after sending <code>PASV</code> and receiving answer from the server <em>&quot;227 Entering passive mode (xxx.xxx.xxx.xxx.198.104)&quot;</em>. curl error is 7, can't connect to host... Could be something related with TLS in the server bad configured?</p>
  <p>Any ideas?</p>