ftp_put()函数问题。 得到警告。 如何解决这个问题?

I'm trying to connect to some other FTP server using PHP and upload a file to it.

On my local machine the code is working absolutely fine but when I run this code from my staging server I got following warning and I'm not able to communicate with the FTP server.

Following is the warning I got.

Warning: ftp_put() [function.ftp-put]: I won't open a connection to 10.140.166.143 (only to 52.232.262.219) in /code/sites/my_web/model/Request.php on line 193

52.232.262.219 is the ip address of my staging server.

The line 193 is as follows:

$file_upload_status = ftp_put($conn_id, $remote_file, $file, FTP_BINARY);

Note: I'm not putting all the code since it's working from my local machine. It's not working only from the staging server.

Is this happening due to the settings in php.ini file. Since php.ini file on my local might have some different setting for this than the php.ini file present on staging server.

FTP server only accepts connections from the IP address 52.232.262.219 and your IP is 10.140.166.143 :(

See the configuration of your ftp server

Warning: ftp_put() [function.ftp-put]: I won't open a connection to 10.140.166.143 (only to 52.232.262.219) in /code/sites/my_web/model/Request.php on line 193

Hard to guess from only this error message, but I assume

  • that you are using passive mode (client wants to connect to server for data connection)
  • and that the server is behind some NAT router, where the server has the internal IP 10.140.166.143 but the router has the the external IP 52.232.262.219

It looks like that the FTP server is not aware, that it is behind a NAT router and sends its private IP address 10.140.166.143 in the response to PASV. But the client has a control connection to the server on 52.232.262.219 (the public IP, probably port forwarded from the router to the server) and thus does not like, that the data connection should be done to a different address.

Fix: use active mode (does not work when client is behind NAT router or firewall too). Some servers can also be configured to announce a different address then their own address. These can be used in passive mode too, provided that the router is configured to forward a large range of ports.