-1

I have been trying for several days to retrieve the list of folders on my ftp servers, despite the various posts consulted I still cannot resolve an error.

I get this error: Warning: ftp_nlist() expects parameter 1 to be resource, bool given in D:\Web\www\Design\index.php on line 10

My code:

<?php
$serveur = '***';
$login = '***';
$pass = '***';

$ftp = ftp_connect($serveur, 21) or die('error connect');
$ftp_login = ftp_login($ftp,$login, $pass) or die('error login');

ftp_nlist($ftp_login, '.');
?>

Has anyone already encountered this problem?

The connection to the server is done well, $ftp & $ftp_login returns true

Thanks!

Darkh
  • 19
  • 8
  • [ftp_login()](https://www.php.net/manual/en/function.ftp-login.php) return a Boolean, not the connection. The connection is stored in `$ftp`. Try `print_r(ftp_nlist($ftp, '.'));` and see what you get. – KIKO Software Mar 18 '23 at 09:27
  • I tried, this time the error disappears, I have a blank page – Darkh Mar 18 '23 at 13:02
  • That means that `ftp_nlist()` didn't return anything, or an error occurred. Check the PHP error log, or [switch on error reporting](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display). – KIKO Software Mar 18 '23 at 13:32
  • The error log gives me this error: `[19-Mar-2023 09:58:55 UTC] PHP Warning: ftp_nlist(): Une tentative de connexion a échoué car le parti connecté n’a pas répondu convenablement au-delà d’une certaine durée ou une connexion établie a échoué car l’hôte de connexion n’a pas répondu. in D:\Web\www\Design\index.php on line 14 [19-Mar-2023 09:58:55 UTC] PHP Stack trace: [19-Mar-2023 09:58:55 UTC] PHP 1. {main}() D:\Web\www\Design\index.php:0 [19-Mar-2023 09:58:55 UTC] PHP 2. ftp_nlist($ftp = resource(2) of type (FTP Buffer), $directory = '.') D:\Web\www\Design\index.php:14` – Darkh Mar 19 '23 at 10:02
  • Ah, that sounds like the FTP server is not responding as expected. Have you tested the server with a normal FTP client, like [FileZilla](https://filezilla-project.org/), _with the same settings_, no SSL, and can you connect and retrieve a directory? – KIKO Software Mar 19 '23 at 10:23
  • This is the first thing I did to be sure of the identifiers, I just did it again to be sure, I have access to my ftp server, instant connection (https://prnt.sc/hEF8uw8Q-QxV) – Darkh Mar 19 '23 at 11:10
  • 1
    It says it is using a TLS connection, so you're not using the same settings. – KIKO Software Mar 19 '23 at 12:38
  • Thank you for the information I had not paid attention to these details, not having my computer because of business trip I would make the changes when I return in a few days. Thanks again for helping me solve this problem – Darkh Mar 20 '23 at 16:16
  • hey, Sorry for the late reply, I therefore tried to connect to the servers in secure mode but also in non-secure, the 2 connections work without worries. I really don't see where the problem could come from. I will try to contact my host in parallel in case he blocks external connections, you never know. – Darkh Mar 26 '23 at 16:12
  • EDIT: Out of curiosity I created a local ftp server with filezilla server, I used the identifiers created and this time I manage to read all the files available – Darkh Mar 26 '23 at 16:24

1 Answers1

0

The ftp_login function returns true if the logi is succesful, false otherwise. You need to pass the ftp instance as the first param of the ftp_nlist function. Currently, you are passing a boolean value.

Your code should be like the following:

<?php
$serveur = '***';
$login = '***';
$pass = '***';

$ftp = ftp_connect($serveur, 21) or die('error connect');
$ftp_login = ftp_login($ftp,$login, $pass) or die('error login');

if(!$ftp_login){
  //login failed
  echo "login failed";
  exit;
}

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

var_dump($files);
exit;
?>

In the above code i added a check to know if the logi is successfull by checking the value present in the $ftp_login variable, if true, the logi is of, otherwise the login has failed.

JDarwind
  • 97
  • 6
  • The connection returns no error, however the page loops without loading anything until you get the following message: bool(false) See here: https://icekom.fr/small.php – Darkh Mar 19 '23 at 09:54