FTP list all directory and sub and files

hi everybody!

Does anyone have any idea to list all the directory and sub-directory and all files on an FTP server in PHP. I already have my list but it just directory. This is my code : [php]

if(($ftp = ftp_connect(“exemple.com”, 21)) == false)
{
echo ‘Erreur de connexion…’;
}

if(!ftp_login($ftp, “aaaa”, “bbbbb”))
{
echo ‘L’identification a échoué…’;
}

$liste_fichiers = ftp_nlist($ftp, ‘.’);

foreach($liste_fichiers as $fichier)
{
echo $fichier. ‘
’;
}

ftp_close($ftp);

[/php]

Well, you need to parse thru the files in one directory and find the directories in them and then loop thru
each. This is not very tricky. Here is a routine that I borrowed from StackOverflow that should do it for you.

[php]
public function ftp_dir_loop($path) {
$ftpContents = ftp_nlist($this->ftp_connection, $path);
foreach ( $ftpContents as $file ) {
if (strpos($file, ‘.’) === false) {
$this->ftp_dir_loop($this->ftp_connection, $file);
}
if (in_array(pathinfo($file, PATHINFO_EXTENSION), $this->accepted_file_types)) {
$this->html_file_paths[$path][] = substr($file, strlen($path) + 1);
}
}
}
[/php]
This is a function that you call and it re-calls itself as needed. You would need to test it to make sure it
returns the file list you really need, but, it should give you a solid starting point.

If the program (web page with your code) is on the same server, you can do it without FTP by using the
function " RecursiveDirectoryIterator "… It works well for on-server code, but, of course does not work
if FTP to another server is needed.

Hope that helps…

Sponsor our Newsletter | Privacy Policy | Terms of Service