ftp_nlist command not working

2019-03-24 01:51发布

问题:

I'm using the following code↓ to connect to a ftp server and get a list of files. It works ok on my local machine(Fedora 11) but not on production (running Ubuntu) where the ftp_nlist method returns false.

$ftpInfo = array('directory' => 'somewebsite.com',
                 'user' => 'someuser',
                 'password' => 'somepass',
                 'port' => 21,
                 'timeout' => 30);
$connectionId = ftp_connect($ftpInfo['directory'], 
                            $ftpInfo['port'], 
                            $ftpInfo['timeout']);

$loginResult = ftp_login($connectionId, $ftpInfo['user'], $ftpInfo['password']);

$files = ftp_nlist($connectionId, '.');

var_dump($files);
ftp_close($connectionId);

Returns an array of files on my machine and false on production.

What makes this particularly annoying is that on both of the cases it manages to connect and login and successfully.

var_dump($loginResult);

returns

bool(true)

回答1:

Turns out this was related to the server's firewall configuration. Switched to passive mode after logging in and it worked ok.

ftp_pasv($connectionId, true);


标签: php ftp