PHP Ftp_Nlist Is Not Working

PHP ftp_nlist is not working

Most typical cause of problems with ftp_nlist (or any other transfer command like ftp_get, ftp_put, ftp_rawlist) is that PHP defaults to the FTP active mode. And in 99% cases, one has to switch to the FTP passive mode, to make the transfer working. Use the ftp_pasv function.

$connect = ftp_connect($ftp) or die("Unable to connect to host");
ftp_login($connect, $username, $pwd) or die("Authorization failed");
// turn passive mode on
ftp_pasv($connect, true) or die("Passive mode failed");

See also my article on the active and passive FTP connection modes.


Further, if your FTP server is reporting an incorrect IP address in the response to the PASV command, you might need to workaround it by using:

ftp_set_option($connect, FTP_USEPASVADDRESS, false);

See PHP FTP + Passive FTP Server Behind NAT.

Though the right solution in this case, is to get the server fixed.


If none of this help, make sure you have tested, if you can even retrieve the directory listing using any commandline/GUI FTP client running on the same machine as your PHP code. You might not have a programming question in the first place.

ftp_nlist command not working

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);

ftp_nlist returning boolean true - not working on server, is on dev

It turns out the firewall on the shared server this code was running on was preventing FTP entering passive mode. Very odd.

ftp_nlist is not working on live

Issue is fixed now. There was problem with PORT on the server.



Related Topics



Leave a reply



Submit