Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ftp_get: Illegal PORT command

Tags:

php

ubuntu

ftp

On 'localhost' I tried to get a file from FTP server and the local file is successfully created. But when I'm trying in Ubuntu server it's displaying there was a problem and file is not downloading into server. Here is the code. And code file created in this location /var/www/html/

<?php
$local_file = 'whdfcleads.csv';
$server_file = 'hdfc/hdfc_leads.csv';
$ftp_server="*********";
$conn_id = ftp_connect($ftp_server)or die("Couldn't connect to $ftp_server");
$ftp_user_name="*****";
$ftp_user_pass="******";
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) {
  echo "Successfully written to $local_file\n";
}
else{
  echo "There was a problem\n";
}
ftp_close($conn_id);
?>

Please help me to solve this issue, in local host it's working fine but in Ubuntu server local file not creating/downloading.

The error is

Array (
    [type] => 2
    [message] => ftp_get(): Illegal PORT command
    [file] => /var/www/html/wftp.php
    [line] => 15
)
like image 370
Sri P Avatar asked Jul 22 '16 12:07

Sri P


People also ask

How to fix 500 Illegal port command?

The 500 is a short error code from FTP - not a port number or anything more meaningful. The standard fix is to enter the command "pass" (passive) after you are connected. This should make the FTP server use your command channel for the returned data too.

What is Port Command in FTP?

In summary, the PORT command is used in FTP to communicate the TCP port number to use for the data transfer channel. In active mode FTP, the client uses the PORT command to tell the server which high-numbered port the client will use for the data channel, and the server opens a connection to that port.

What is FTP passive port range?

Note: The system enables passive ports 49152 through 65534 for Pure-FTPd servers and ProFTPD servers by default.

What is FTP passive mode?

Passive FTP is an FTP mode that can be requested by a client to alleviate the issues caused by client-side firewalls. Both the server and the client must support passive FTP for this process to work. When passive FTP is used, the client will initiate the connection to the server.


1 Answers

The "Illegal PORT command" is a message issued by ProFTPD server, when it receives PORT command with an invalid IP address.

What typically happens, when the client is behind a NAT and reports its internal IP address to the server, not knowing the server is not able to reach back to that IP address.

The easiest (and generally correct) solution is to use an FTP passive mode, instead of an active mode (the default in PHP).

In PHP, you switch to the passive mode by calling the ftp_pasv function after the ftp_login:

...
ftp_login($conn_id, $usr, $pwd) or die("Cannot login");
ftp_pasv($conn_id, true) or die("Cannot switch to passive mode");
...

See my article on FTP connection modes to understand, what the active and passive mode means, and why everyone uses the passive mode nowadays.

like image 107
Martin Prikryl Avatar answered Oct 18 '22 21:10

Martin Prikryl