phpftp

PHP FTP fails with error ftp_nlist(): php_connect_nonb() failed: Operation now in progress (115)


I have a problem with trying to send files using a PHP FTP function.

I am attempting to send files (PDFs) from a webserver (UNIX) to a Windows server in an office using a PHP function running on the webserver. FTP has been installed on the Windows server.

The function connects to the office server and logs in ok. However, as soon as the function attempts to do anything (eg list the content of the office server directory) I get an error (such as... ftp_nlist(): php_connect_nonb() failed: Operation now in progress (115)). The destination server logs an error: [Administration Server] Couldn't bind on 192.168.1.1:14148. Reason: EADDRINUSE - Local address in use. Retrying in 1 seconds. I have tried looking these errors up but have ben unable to find anything that helps me.

The connection needs to be passive and from other answers in SO I believe that it is important to stop the passive mode using the wrong IP address. So that is in there now too (ftp_set_option($ftp_conn, FTP_USEPASVADDRESS, false)) but I still get the same error.

I get the same problem when using an SSL or non-SSL connection.

I have managed to get a response to ftp_systype which tells me "UNIX".

Other people have talked on SO about intermittent problems that cause this error (eg "php_connect_nonb() failed: Operation now in progress (115)" happens intermittently) but for me it is a constant error. I have checked and the FTP server is set to return the external IP address when passive mode is established. I also understand that there were some issues in older versions of PHP. I have PHP v7.4 on the webserver.

I can connect (and copy files) without problems using Filezilla and WinSCP, just not when using the PHP function.

The PHP function contains:

$ftp_conn = ftp_ssl_connect($ftp_server, $ftp_port); // response is resource(2) of type (FTP Buffer)
$login = ftp_login($ftp_conn, $username, $password);
$result = ftp_set_option($ftp_conn, FTP_USEPASVADDRESS, false);
$result = ftp_pasv($ftp_conn, true);
echo '<br>The system type is: '.ftp_systype($ftp_conn); // The system type is: UNIX
$already_there = ftp_nlist($ftp_conn, '.'); // this triggers the error!

I hope that somebody can point me in the right direction or suggest something else for me to try.

Many thanks in advance!


Solution

  • Make sure the hosting of your webserver allows outgoing FTP data connections.

    FTP data connections (in the passive mode) need separate port range opened in the firewall, not just the control connection port 21.

    See my article on FTP connection modes to details.