Documentation on ftp_nlist

ftp_nlist = Returns a list of files in the given directory

ftp_stream The link identifier of the FTP connection. directory The directory to be listed. This parameter can also include arguments, eg. ftp_nlist($conn_id, "-la /your/dir"); Note that this parameter isn't escaped so there may be some issues with filenames containing spaces and other characters.

Usage, params, and more on ftp_nlist

array ftp_nlist ( resource $ftp_stream , string $directory )

ftp_stream The link identifier of the FTP connection. directory The directory to be listed. This parameter can also include arguments, eg. ftp_nlist($conn_id, "-la /your/dir"); Note that this parameter isn't escaped so there may be some issues with filenames containing spaces and other characters.

Returns an array of filenames from the specified directory on success or FALSE on error.

Notes and warnings on ftp_nlist

Basic example of how to use: ftp_nlist

Example #1 ftp_nlist() example

<?php

// set up basic connection
$conn_id ftp_connect($ftp_server);

// login with username and password
$login_result ftp_login($conn_id$ftp_user_name$ftp_user_pass);

// get contents of the current directory
$contents ftp_nlist($conn_id".");

// output $contents
var_dump($contents);

?>

The above example will output something similar to:

 array(3) { [0]=> string(11) "public_html" [1]=> string(10) "public_ftp" [2]=> string(3) "www" 

Other code examples of ftp_nlist being used

Hi,

I wrote this function to get the file list, I tested it only with php5 with linux ftp. And works fine for me.

it will return an array with the name, type (file/folder), owner, owner_id and rights.

<?php
   
function ftp_get_filelist($con, $path){
       
$files = array();
       
$contents = ftp_rawlist ($con, $path);
       
$a = 0;

        if(
count($contents)){
            foreach(
$contents as $line){

               
preg_match("#([drwx\-]+)([\s]+)([0-9]+)([\s]+)([0-9]+)([\s]+)([a-zA-Z0-9\.]+)([\s]+)([0-9]+)([\s]+)([a-zA-Z]+)([\s ]+)([0-9]+)([\s]+)([0-9]+):([0-9]+)([\s]+)([a-zA-Z0-9\.\-\_ ]+)#si", $line, $out);

                if(
$out[3] != 1 && ($out[18] == "." || $out[18] == "..")){
                   
// do nothing
               
} else {
                   
$a++;
                   
$files[$a]['rights'] = $out[1];
                   
$files[$a]['type'] = $out[3] == 1 ? "file":"folder";
                   
$files[$a]['owner_id'] = $out[5];
                   
$files[$a]['owner'] = $out[7];
                   
$files[$a]['date_modified'] = $out[11]." ".$out[13] . " ".$out[13].":".$out[16]."";
                   
$files[$a]['name'] = $out[18];
                }
            }
        }
        return
$files;
    }
?>

Greets,

Jacob

How to get the file list in alphabetical order with all directories at the top:

<?php
$ftp_nlist
= ftp_nlist($ftp_connect, ".");

//alphabetical sorting

sort($ftp_nlist);
foreach (
$ftp_nlist as $v) {

//1. Size is '-1' => directory
 
if (ftp_size($ftp_connect, $v) == -1) {

//output as [ directory ]
     
echo "[ " . $v . " ]<br />\n";
  }
}
foreach (
$ftp_nlist as $v) {

//2. Size is not '-1' => file
 
if (!(ftp_size($ftp_connect, $v) == -1)) {

//output as file
     
echo "" . $v . "<br />\n";
  }
}
?>

Result looks like:

[ a_mydirectory ]
[ b_mydirectory ]
[ c_mydirectory ]
...
a_myfile.htm
b_myfile.css
c_myfile.php
...

A little correction to king_killa at juggalos4life dot com's note, it isn't ftp_res, it's ftp_size.

By the way, here is a complete function that will get all the files on a server, then write them to a array.

<?php
$ftp1
= ftp_connect('ftp.nowhere1230404.foo') or die("Couldn't connect to server");
ftp_login($ftp1,'foo','bar');
ftp_pasv($ftp1, TRUE); //Passive Mode is better for this

//Get them file!
echo "Collecting Files on Neodecoder<br>";
//Set defaults just in case. PHP complains anyway if we don't.
$dir = "";

function
filecollect($dir,$filelist) {
global
$ftp1; //Get our ftp
$files = ftp_nlist($ftp1,$dir); //get files in directory
foreach ($files as $file) {
$isfile = ftp_size($ftp1, $file);
if(
$isfile == "-1") { //Is a file or directory?
 
$filelist = filecollect($dir.'/'.$file,$filelist,$num); //If a folder, do a filecollect on it
}
else {
 
$filelist[(count($filelist)+1)] = $file; //If not, add it as a file to the file list
}
}
return
$filelist;
}

$filelist = filecollect($dir,$filelist);

echo
"<pre>";
print_r($filelist);
echo
"</pre>";
?>

This is really handy if your trying to transfer all the files from a ftp server to another server. Which, is why I made the script in the first place. heh..

ftp_nlist() or ftp_rawlist() takes ages then returns nothing...

If you are having this issue, you may need to enable PASV mode FTP transfers using the ftp_pasv() function.

Example...

<?php
$ftp_host
= "yourFTPHost";
$ftp_user = "yourUsername";
$ftp_password = "yourPassword";

//Connect
echo "<br />Connecting to $ftp_host via FTP...";
$conn = ftp_connect($ftp_host);
$login = ftp_login($conn, $ftp_user, $ftp_password);

//
//Enable PASV ( Note: must be done after ftp_login() )
//
$mode = ftp_pasv($conn, TRUE);

//Login OK ?
if ((!$conn) || (!$login) || (!$mode)) {
   die(
"FTP connection has failed !");
}
echo
"<br />Login Ok.<br />";

//
//Now run ftp_nlist()
//
$file_list = ftp_nlist($conn, "");
foreach (
$file_list as $file)
{
  echo
"<br>$file";
}

//close
ftp_close($conn);

?>

The function from nekrostar at gmx dot net does not work for me, so I've tryed to do it my way. For checking if a found file is a directory or not I use the ftp_size function.

<?php
function ftplistdir($conn_id, $dir){
   
$fold_no = array(".", "..", "cgi-data", "comp", "zuern", "counter");
   
$list = ftp_nlist( $conn_id, $dir );
    foreach(
$list as $file){
        if (
ftp_size($conn_id, $dir ."/".$file)== -1){
            if (
in_array($file, $fold_no)) {
                print
$file ." Ueberspringe ausgeschlossenes Verzeichnis.<br>";
            } else {
               
$geslist[]= $dir ."/". $file;
               
$temp=ftplistdir($conn_id, $dir ."/". $file);
               
$geslist=array_merge($geslist, $temp);
            }
        }else{
           
$geslist[]= $dir ."/". $file;
        }
    }
    return
$geslist;
}
?>
I hope it's usefull for anyone
Robert

Recursive directory delete using ftp_nlist() ...
<?php
function ftp_rmdirr($path, $handle)
    {
    if (!(@
ftp_rmdir($handle, $path) || @ftp_delete($handle, $path)))
        {
       
$list = ftp_nlist($handle, $path);
        if (!empty(
$list))
            foreach(
$list as $value)
               
ftp_rmdirr($value, $handle);
        }
    @
ftp_rmdir($handle, $path);
    }
?>
It would be very useful if it was built into php. After all most of the time we want to remove non empty directories too. I bet everyone out there dealing with the file system had faced this problem.

To use custom LIST - flags, for example
LIST -aF ../*/
(listing protected pub dirs)
You can use
<?php
$list
= ftp_nlist($ftp, "-aF ../*/");
?>
Took me a while to clever that one out :-)