How to get ftp_nlist() php function working?

1k Views Asked by At

I want to list files (log files in .txt) in a directory from a server and I can't get ftp_nlist() function working.

First, the function ftp_nlist() was returning false, I have searched on the internet and found that the passive mode must be enabled due to firewall or NAT routers, I have enabled passive mode.

The code :

include("/var/www/luvinsastroi.mtxserv.com/public_html/logs/password_protect.php");
include 'config.php';
$ftp_connection = ftp_connect($host,$port);

ftp_login($ftp_connection, $user, $password);
if ($passiveftp == 1) {
    ftp_pasv($ftp_connection, true);
}
echo "<b>Folder:</b> ".$path."<br>";
if (ftp_chdir($ftp_connection, "".$path."")) {
    echo "Successfully accessed log folder.";
} else { 
    echo "Couldn't access log folder";
}
$contents = ftp_nlist($ftp_connection, ".");

I expect the listing of all files contained in $path folder (log files) and nothing append (without the passive mode) and with the passive mode I'm getting the error : Warning: ftp_nlist(): php_connect_nonb() failed: Operation now in progress (115)

with the line pointer on "$contents = ftp_nlist($ftp_connection, ".");" and no file is listed.

1

There are 1 best solutions below

0
jj_dev2 On
ftp_set_option($ftpconn, FTP_USEPASVADDRESS, false);

This line of code before setting passivity of the connection ftp_pasv($ftpconn, true);

Solved my problem, maybe it's the same for you if it isn't a firewall issue.