showkey
showkey

Reputation: 358

Warning: ftp_put(): Connecting to port

I cannot upload files into my ftp server.

There is always warning:

ftp_put(): Connecting to port.

<?php
    set_time_limit(0);
    $host = 'xxxx';
    $usr = 'yyyy';
    $pwd = 'zzzz';
    $local_file = '/home/back.sql';
    $ftp_path = '/public_html/';
    $conn_id = ftp_connect($host, 21) or die ("Cannot connect to host");
    ftp_login($conn_id, $usr, $pwd) or die("Cannot login");
    $upload = ftp_put($conn_id, $ftp_path.'back1.sql', $local_file, FTP_ASCII); 
    print($upload);
?>

The code were executed for three times. I got three different warnings.

Warning: ftp_put(): Connecting to port 1926 in filename (i named it) on line 10
Warning: ftp_put(): Connecting to port 1928 in filename (i named it) on line 10
Warning: ftp_put(): Connecting to port 1930 in filename (i named it) on line 10

  1. What does the warning info mean?
  2. Why connect to different ports? Maybe the ports should be 21 for every time, why not?

Upvotes: 2

Views: 5151

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202292

The "Connecting to port xxx" is a message issued by PureFTPD server, when it tries to connect back to the FTP client to its active mode data connection port (which is random, that's why it changes).

If you really need to use the active mode, you need to allow incoming connections to the active mode data connection port range used by PHP.
See my guide for network configuration necessary for the active mode FTP.

Though, if you do not need to use the active mode, use the passive mode instead. The passive mode generally does not require any network configuration on a client side.

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 the above guide to understand the difference between the active and the passive FTP mode.

Upvotes: 3

Related Questions