List ftp folder with php

Asked

Viewed 1,603 times

0

Good afternoon guys, can you help? I want to list all the files in a folder of an ftp connection with php, I am using this code:

// Dados do servidor
$servidor = 'server'; // Endereço
$usuario = 'user'; // Usuário
$senha = 'pass'; // Senha

// Abre a conexão com o servidor FTP
$ftpconn = ftp_connect($servidor); // Retorno: true ou false

// Faz o login no servidor FTP
$login = ftp_login($ftpconn, $usuario, $senha); // Retorno: true ou false

// Informa sucesso ou não da conexão
if (isset($login)) {
    echo "conectado";
}else{
    echo "não conectado";
}

// Recebe lista dos arquivos do ftp
$lista = ftp_nlist($ftpconn, '/');

// Imprime var_dump
var_dump($lista);

// Imprime foreach
foreach ($lista as $item) {
    echo $item . "<br />";
}

// Fecha conexão ftp
ftp_close($ftpconn);

Using ftp_rawlist or ftp_nlist, vardump returns me: bool(false), and print_r returns nothing. They can help?

The connection is successful, I can create folder, delete, etc. The problem is only with the same list...

  • The code seems to be right, try to list the current directory instead of / to see what happens. $lista = ftp_nlist($ftpconn, '.');

  • Hello friend, thanks for the answer. But problem persists. I also tried to list another folder and nothing. &#xA;$lista = ftp_nlist($ftpconn, '.');&#xA;$lista = ftp_nlist($ftpconn, '');&#xA;$lista = ftp_nlist($ftpconn, '/novapasta/');&#xA;$lista = ftp_nlist($ftpconn, 'novapasta/');

  • Is there a read file in the folder? Believe me, it is possible to write without reading permission.

  • Accessing through Ilezilla with the same credentials, I have full access to the folders and files. By checking permissions, they are 755 for all folders. Some additional information, I executed this code in apache5 and 7. Both report the same result.

  • Well, the need was to list these ftp files and then do some actions on them. I thought about the idea of downloading all the ftp files to a local folder, list them and then do what is necessary. I will do it, having successfully put the code here for all

1 answer

1


Try enabling passive listing mode:

// Habilita o modo Passivo
ftp_pasv($ftpconn, true);

// Recebe lista dos arquivos do ftp
$lista = ftp_nlist($ftpconn, '/');

// Imprime var_dump
var_dump($lista);
  • Success! Thank you very much friend!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.