PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  
<ftp_nb_putftp_pasv>
view the version of this page
Last updated: Thu, 21 Aug 2003

ftp_nlist

(PHP 3>= 3.0.13, PHP 4 )

ftp_nlist -- Returns a list of files in the given directory

Description

array ftp_nlist ( resource ftp_stream, string directory)

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

Example 1. ftp_nlist() example

<?php

// set up basic connection
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// check connection
if ((!$conn_id) || (!$login_result)) {
    die("FTP connection has failed !");
}

// get contents of the root directory
$contents = ftp_nlist($conn_id, "/");

// print each entry
foreach ($contents as $entry) {
    echo $entry, "<br />\n";
}

?>

See also ftp_rawlist().



add a note add a note User Contributed Notes
ftp_nlist
tobrien at florahill dot vic dot edu dot au
13-Oct-2003 10:27
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($connTRUE);

//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);

?>
aRc
15-Jul-2003 02:46
in windows, ftp_nlist will die on a directory with a space in it's name.  to get around this, use ftp_chdir:

//change directory
ftp_chdir($conn, "directory with spaces");
//then blindly list
$contents = ftp_nlist($conn, "");
neodeus at allesamarsch dot com
04-Jun-2003 12:10
This is an Example tp read out a Complete FTP Server ( beginn at your root dir )
    function files($dir)
    {
        /*
        if(!isset($dir) || empty($dir))
        {
            $dir=ftp_pwd($this->conn_id);
        }
        */
        unset($list);
        unset($files);
        unset($folders);
        unset($folder);
        unset($file);
        @ftp_chdir($this->conn_id, $dir);
        $dir = ftp_pwd($this->conn_id);
        $list=Array();
        $list=ftp_nlist($this->conn_id, "$dir");
        $files = Array();
        $folders = Array();
        for($i = 0; $i != sizeof($list); $i++)
        {
            $entry = str_replace("//", "", $list[$i]);
            if(@ftp_chdir($this->conn_id, $entry))
            {
                $folders[] = $entry;
                ftp_chdir($this->conn_id, $dir);
            }
            else
            {
                $files[] = $entry;
            }
        }
        print "\t<b> Dateien in ".$dir." :</b><br><br>";
        foreach($files as $file)
        {
            print $file."<br>";
        }
        print "\t<b>Ordner in ".$dir." :</b> <br><br>";
        foreach ($folders as $folder)
        {
            print "\t".$folder."<br>";
            ftp_chdir($this->conn_id, $dir);
            $this->files($folder);
        }
    }
zanzamarr at yahoo dot com dot au
04-May-2003 01:22
To have a list with all files in a choosed folder use this code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Connessione</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="stile.css" />
</head>

<body>
<?Php
$ftp_server 
$_POST['host'];
$ftp_user_name $_POST['nome_utente'];
$ftp_user_pass $_POST['password'];

$conn_id ftp_connect($ftp_server); 

$login_result ftp_login($conn_id$ftp_user_name$ftp_user_pass); 

if ((!
$conn_id) || (!$login_result)) { 
        echo 
"La connessione FTP ?fallita!";
        echo 
"Tentativo di connessione a $ftp_server per l'utente $ftp_user_name"
        die; 
    } else {
        echo 
"Connesso a $ftp_server, utente $ftp_user_name";
    }
   
   
$cartella ftp_pwd($conn_id);
    if (
$cartella "/"){
        echo 
"<br />
            Sei nella cartella : Home<br /><br />"
;
    }else{
    echo 
"<br />
            Sei nella cartella : $cartella<br />"
;
    }
   

$form "<form action=\""$_SERVER["PHP_SELF"] ."\" method=\"POST\"> <select>";
$list = Array(); 
$list ftp_nlist($conn_id"$cartella");
while(list(
$chiave,$valore) = each($list)) {
$dir $conn_id."/".$valore;
if( !
is_dir($dir) && $dir != "." && $dir != "..") {
 
$form .= "<option>".$valore."</option><br />";
}
}
$form .= "</select></form>";
echo 
$form;
?>

</body>
</html>
louis at ewens dot com
30-Apr-2003 06:03
If you do a wildcard or other search for a particular file and the file doesn't exist, Solaris FTP servers send back "search*.xml: No such file or directory" as a single element in the response array. This makes it hard to check for the presence of a file.

One possible work-around is to parse your own raw directory listing. Or you could check for the error verbatim. But different servers could return different values. A simpler way to compensate is to use the ftp_size command to validate the returned file:

if ( ($filelist = ftp_nlist( $this->conn_id, $filesearch )) === FALSE )
{
       Error( "Could not get file list" );
       return FALSE;
}
// File actually exists? Catches invalid FTP server list responses
if ( !count($filelist) || ftp_size( $this->conn_id, $filelist[0] ) == -1 )
{
       Error( "No valid files" );
       return FALSE;
}
// File is really there, fetch it or whatever else you want to do
rfr at ajato dot com dot br
13-Feb-2003 12:25
There are some FTP servers that just dont accept the ftp_nlist command, they return a bool variable. but I think all of them accept the LIST command which is the ftp_rawlist command. So yu have to parse rawlist to nlist. I found a code here posted by "fredrik" but it shows a bug if the files at the ftp server have spaces oon theeir name... so here its a better function:
function rawlist_2_nlist($list)
{
    $newlist = array();
    reset($list);
    while (list(,$row) = each($list))
    {
        $buf="";
        if ($row[0]=='d'||$row[0]=='-'){
        $buf = substr($row,55);
        $newlist[]=$buf;
        }
    }

return $newlist;
}
ryan at wonko dot com
21-Mar-2002 11:31
A note to developers using PHP on Windows servers: as of PHP 4.1.2, ftp_nlist() is broken in the Windows build of PHP. It will return nothing at all, even if the same code works fine on UNIX. So if you're going crazy trying to figure out why the function isn't returning anything, stop wasting your time, you're not doing anything wrong. Hopefully this will get fixed in future versions, although it's apparently been an issue since at least 4.0.6.

More info on this bug is at http://bugs.php.net/bug.php?id=16057
21-Sep-2001 05:57
for me ftp_nlist doesn't always work... sometimes the call to ftp_nlist takes AGES before it ends (and returns NOTHING)... I therefor recommend you use rawlist in stead of nlist...

you can use this function to convert a rawlist array into a nlist-style array

        function rawlist_2_nlist($list)
        {
            $newlist = array();
           
            @reset($list);
            while (list(,$row) = @each($list))
            {
$index = strrpos($row, " ");
            $newlist[] = substr($row, $index+1);
            }
            return $newlist;
        }
grios at pucp dot edu dot pe
13-Jan-2001 05:22
When I use nlist under a RedHat 6.1 it list the directories, but if use it with RedHat 6.1 does not list directories. However the problem is in the version of the ftp server, not in the nlist command. Test with ftp client under DOS and execute nlist
markw at altexa dot com
14-Nov-2000 04:33
You can do a wildcard file listing with ftp_nlist, as in:

$list=ftp_nlist($conn_id, "$dir/*.txt");

<ftp_nb_putftp_pasv>
 Last updated: Thu, 21 Aug 2003
show source | credits | sitemap | mirror sites 
Copyright © 2001-2003 The PHP Group
All rights reserved.
This mirror generously provided by: http://php.mirrors.ilisys.com.au/
Last updated: Sat 01 Nov 2003 04:13:36 EST EST