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
Laravel 5.4 on PHP 7.0: Pdo Exception - Could Not Find Driver (Mysql)
Fetching Single Row, Single Column with Pdo
Single Quotes or Double Quotes for Variable Concatenation
Soap PHP Fault Parsing Wsdl: Failed to Load External Entity
Advantages of MySQLi Over MySQL
How to Schedule the Execution of a PHP Script on the Server Side
Convert Jpg Image to Gif, Png & Bmp Format Using PHP
Algorithm That Will Take Numbers or Words and Find All Possible Combinations
Caching Http Responses When They Are Dynamically Created by PHP
How to Check If a Url Exists in PHP
Python Equivalent of PHP's Compact() and Extract()
Calculate Difference Between Two Dates Using Carbon and Blade
Jquery Ajax Request with JSON Response, How To