0

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.

Lucas Damase
  • 83
  • 1
  • 2
  • 10
  • Is any FTP client, running on the same machine as your PHP script, able to retrieve the directory listing from the FTP server? – Martin Prikryl Jul 21 '19 at 13:16
  • @MartinPrikryl I use FileZilla for Windows and the machine of the PHP script and the machine I triying to retrieve directory are both running on linux and hosted on https://mtxserv.com/ – Lucas Damase Jul 21 '19 at 13:30
  • That does not answer my question. – Martin Prikryl Jul 21 '19 at 13:44
  • The thing is that there's no FTP client installed on these machine because I don't have SSH access on these machine, one is gameserver (the machine i'm trying to retrieve a directory) and one is web server. I have already contacted the host but he maintains me that there's not a firewall error. – Lucas Damase Jul 21 '19 at 14:06
  • Firewall, NAT or misconfigured FTP server are the most likely explanations for this problem. -- Did you check the return value of `ftp_pasv`? -- Post at least a verbose FileZilla log file. – Martin Prikryl Jul 21 '19 at 14:20
  • `ftp_pasv` return me '1'. The link to the FileZilla server connection verbose : https://pastebin.com/eKNkGS2t – Lucas Damase Jul 21 '19 at 14:23
  • Looks good. So it's most likely a firewall issue. – Martin Prikryl Jul 21 '19 at 14:26
  • And is there an other way to list in an array a files in a directory? Because the host respond me that there's no firewalling rules on it. – Lucas Damase Jul 22 '19 at 11:10
  • If FTP is your only API, there's no other way that would not have the same problem, which you have with `NLST` (`ftp_nlist`). – Martin Prikryl Jul 22 '19 at 11:11
  • I've seen on other thread that adding `ftp_set_option($ftp_connection, FTP_USEPASVADDRESS, false);` can fix the problem, this function returns me 1 but when I do `ftp_get_option($ftp_connection, FTP_USEPASVADDRESS);` this function returns me null – Lucas Damase Jul 22 '19 at 16:11
  • Don't you use `echo` to print the value? That would indeed you `1` instead of `true` and an empty output instead of `false`. What would match the behavior you are seeing. – Anyway, I do not think the option would help you. The FileZilla log indicates that the server uses correct IP address for `PASV` response (that's why I've asked you for the log). – Martin Prikryl Jul 22 '19 at 18:39

1 Answers1

0
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.

Alexander van Oostenrijk
  • 4,644
  • 3
  • 23
  • 37
jj_dev2
  • 451
  • 4
  • 7