PHP upload via FTP - ftp_put() I won't open a connection to x.x.x.x (only to y.y.y.y)

The error message is actually very relevant.

It's Pure-FTPd error response to PORT command, when the client is asking the server to open a data connection to an IP address different to that of the client (as the server sees it). So it's a remote error message sent by the server, not a local error message produced by PHP.

The problem is typically caused by the client knowing only its local IP address, not the external IP address.


Now, the question is why PHP uses PORT, when you asked it to connect using passive mode (ftp_pasv).

It's clearly because you call ftp_pasv before even logging in.

So the server rejects the PASV call with:

530 You aren't logged in

Unfortunately the PHP does not propagate error messages from PASV call. And it silently falls back to the default active mode. You can tell the call failed by its return value (that you do not check).


Just move the ftp_pasv call after the ftp_login.

$conn_id = ftp_connect('50.xx.xx.xx');

ftp_login($conn_id, 'user', 'pass');

ftp_pasv($conn_id, true);

The documentation clearly suggests it:

Please note that ftp_pasv() can only be called after a successful login or otherwise it will fail.


And of course, you should do a better error checking.


For a similar issue (just with ProFTPd), see:
PHP ftp_put returning "Unable to build data connection: Connection refused"

Tags:

Php

Ftp