Vincent Pazeller
Vincent Pazeller

Reputation: 1508

FTPS login succeeds but put fails

Since I lost a half day solving this, I believe this is worth sharing my issue (and solution) here.

Here is my initial code:

$host = ...
$user = ...
$pass = ...
$conn = ftp_ssl_connect($host, 22);
if($conn === false) {
    die('could not connect :(');
}

$login_result = ftp_login($conn, $user, $pass);
ftp_pasv($conn, true);
$file = realpath('test.txt');
echo 'putting file '.$file."\n";
ftp_put($conn, 'test.txt', $file, FTP_BINARY);
ftp_close($conn);

Unfortunately, this did not work, even if the connection succeeds. Here is the message I got from the fpt_put line after hanging for roughly one minute:

Warning: ftp_put(): php_connect_nonb() failed: Operation now in progress (36)

Note that this is not a passive toggle issue (I tried with and without), as I could see in most similar issues.

N.B.: I know that usually port 21 is used, but when you are working with 3rd parties, you may not have any choice.

Upvotes: 1

Views: 2476

Answers (1)

Vincent Pazeller
Vincent Pazeller

Reputation: 1508

So here is the solution and a big thanks to this blog post: http://www.elitehosts.com/blog/php-ftp-passive-ftp-server-behind-nat-nightmare/ which showed me the right way...

Basically, it looks like the fix has been added to PHP and the FTP_USEPASVADDRESS option does the trick, that is, set the option right before switching to passive mode:

$host = ...
$user = ...
$pass = ...
$conn = ftp_ssl_connect($host, 22);
if($conn === false) {
    die('could not connect :(');
}

$login_result = ftp_login($conn, $user, $pass);
//ADD THIS ⬇︎
ftp_set_option($conn, FTP_USEPASVADDRESS, false);
//ADD THIS ⬆︎
ftp_pasv($conn, true);
$file = realpath('test.txt');
echo 'putting file '.$file."\n";
ftp_put($conn, 'test.txt', $file, FTP_BINARY);
ftp_close($conn);

And this works :D. Hope this will help someone...

Upvotes: 2

Related Questions