phpftppassive-mode

PHP timeout on put/get/list commands


I've been dealing with this problem all day long. A couple hours ago I wrote this Question. I've got a PHP script that works OK in my development PC but on the real server are not quite good.

The script is:

$conn_id = ftp_ssl_connect($ftp_server); 
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 

//$DB = @mysqli_connect($hostname_DB,$username_DB,$password_DB,$database_DB); 

if ($login_result == null)
    die("Cannot connect");   
echo "Connected<br />Changing to passive mode <br />"; 

if (ftp_pasv($conn_id, true)) 
{
    echo "Passive mode: ON<br /><br />";
}
else
{
    echo "Passive mode: OFF<br /><br />";
}


echo "Current dir: " . ftp_pwd($conn_id) . "\n";

if (ftp_chdir($conn_id, "ABC")) {
    echo "Current dir is now: " . ftp_pwd($conn_id) . "\n";
} else { 
    echo "Dir was not changed\n";
}

$files = array();
$files = ftp_rawlist($conn_id, '.'); 
echo "<br />Files<br />";
print_r($files);

ftp_close($conn_id);

On this line: $files = ftp_rawlist($conn_id, '.'); (line 65) the script freezes about a minute or so and gives this error:

Connected
Changing to passive mode 
Passive mode: ON

Current dir: / Current dir is now: /ABC
Warning: ftp_rawlist(): php_connect_nonb() failed: Operation now in progress (115) in /var/www/html/ftpscript.php on line 65

Files

Without changing into passive mode is the same output without the warning. No files are echoed either (below 'Files'). The same happend with any command like nlist, mput, mget, etc.

I read this article but didn't know if it still applies. Others sites says is my firewall configuration but I don't understand what to do. My server is running:

PHP 5.4.16 (cli) (built: Oct 31 2014 12:59:36) 
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies

I opened ports 20, 21 and range from 5000 to 5100. With all these I don't know if is a problem of my end but PHP. If I use lftp (cli ftp client) it works OK.

EDIT #1

It seems it's a bug in the ftp implementation in php. However I don't know how to apply the patch listed here https://bugs.php.net/patch-display.php?bug_id=55651&patch=ftp_usepasvaddress.patch&revision=latest

Please, any help will be appreciated. Thanks!


Solution

  • Well, I ended writing function to download/upload and listing using curl with PHP. AFAIK It's a PHP bug when using the built-in ftp implementation. Curl is a bit slower but it's ok for my needs.