(PHP 4, PHP 5)
ftp_nlist — 返回给定目录的文件列表
$ftp_stream
, string $directory
)
ftp_stream
FTP 连接资源。
directory
指定要列表的目录。本参数接受带参数的形式,例如: ftp_nlist($conn_id, "-la /you/dir"); 注意此参数不对传入值做处理,在目录或者文件名包括空格或特殊的情况下,可能会存在问题。
如果成功则返回给定目录下的文件名组成的数组,否则返回 FALSE
。
Example #1 ftp_nlist() 例子
<?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, "/");
// output $contents
var_dump($contents);
?>
以上例程的输出类似于:
array(3) { [0]=> string(11) "public_html" [1]=> string(10) "public_ftp" [2]=> string(3) "www"
Jacob Slomp (2012-05-02 03:58:38)
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
vijay dot mahrra at fronter dot com (2011-03-17 02:35:36)
We needed to get a list of all files in all subdirectories and couldn't see a method to do this so we wrote our own:
<?php
/**
* ftpRecursiveFileListing
*
* Get a recursive listing of all files in all subfolders given an ftp handle and path
*
* @param resource $ftpConnection the ftp connection handle
* @param string $path the folder/directory path
* @return array $allFiles the list of files in the format: directory => $filename
* @author Niklas Berglund
* @author Vijay Mahrra
*/
function ftpRecursiveFileListing($ftpConnection, $path) {
static $allFiles = array();
$contents = ftp_nlist($ftpConnection, $path);
foreach($contents as $currentFile) {
// assuming its a folder if there's no dot in the name
if (strpos($currentFile, '.') === false) {
ftpRecursiveFileListing($ftpConnection, $currentFile);
}
$allFiles[$path][] = substr($currentFile, strlen($path) + 1);
}
return $allFiles;
}
?>
zgardner at allofe dot com (2010-11-28 17:46:00)
While running WAMP (Vista, Apache 2.21, MySQL 5.1.36, PHP 5.3) and recursively downloading files from a LAMP FTP server (Ubuntu 8.04, Apache 2.2.8, MySQL 5.1.51a, PHP 5.2.4, VSFPTD), I would find that my script stops downloading files after a while. I traced it to the call to ftp_nlist halting the script.
To solve this, I did a ftp_pasv($ftp, true). There's nothing in the VSFTPD, Apache, or PHP error logs on either the client or the server. Very strange.
Estigy (guenther.stadler [at] gmx.net) (2010-09-02 02:41:36)
I experienced that with certain versions of FTP servers, passing additional parameters to ftp_nlist can change the format of the list you get back.
Example:
<?php
$arrFiles = ftp_nlist($connId, '-la ' . $dstDir);
?>
While one of my servers returned only the list of filenames (as expected), the other one returned a list consisting not only of filenames, but looking like the output of ftp_rawlist - including rights, filesize, creation date etc.
Using the paramter "-a" instead of "-la" changed this, both servers returned only filenames.
Anonymous (2009-12-29 09:39:52)
Some complain that ftp_nlist, always return FALSE. I did experience this behavior myself, until I used ftp_pasv, which is useful if your client is behind a firewall (which most clients are now), then ftp_nlist worked just fine. I don't really know what are all the implications of using ftp_pasv, but if you read or experience that ftp_nlist, ftp_get, ftp_nb_get doesn't work, try adding the following:
ftp_pasv($conn_id,true);
juhakala at gmail dot com (2008-05-07 03:23:12)
I noticed that this function won't work if working path is different, because $dir keeps value of whole path string. That's why adding basename()-function strips the path and this function will work also sub folders, not only in root folder.
function ftp_is_dir($dir) {
global $ftp_connect;
if (ftp_chdir($ftp_connect, basename($dir))) {
ftp_chdir($ftp_connect, '..');
return true;
} else {
return false;
}
}
webmaster at weltvolk dot de (2007-10-12 06:28:18)
In order to make file/folder separation work on remote servers that do not support ftp_size() it's better to use the function ftp_is_dir() mentioned below:
<?php
//identify directories
function ftp_is_dir($dir) {
global $ftp_connect;
if (ftp_chdir($ftp_connect, $dir)) {
ftp_chdir($ftp_connect, '..');
return true;
} else {
return false;
}
}
$ftp_nlist = ftp_nlist($ftp_connect, ".");
//alphabetical sorting
sort($ftp_nlist);
foreach ($ftp_nlist as $v) {
//1. ftp_is_dir() is true => directory
if (ftp_is_dir($v)) {
//output as [ directory ]
echo "[ " . $v . " ]<br />\n";
}
}
foreach ($ftp_nlist as $v) {
//2. ftp_is_dir() is false => file
if (!ftp_is_dir($v)) {
//output as file
echo "" . $v . "<br />\n";
}
}
?>
webmaster at weltvolk dot de (2007-10-05 02:24:45)
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
...
wim dot dupre at skynet dot be (2007-08-20 17:59:35)
php 5.2.2 on MAC OS 1.4 (Tiger) seems to defaut to active ftp. to get ftp_nlist functioning, i needed to force ftp to passive.
the default behaviour with PHP 5.2.3 on windows XP seems to be passive ftp.
greetz,
wim
ralph at ralphje dot nl (2007-08-16 03:13:27)
If you receive an error due some open_basedir restrictions, use the following:
<?php
putenv('TMPDIR=/tmp/');
$filelist = ftp_nlist($ftp, "./accounts");
?>
(if /tmp/ is an allowed directory within open_basedir)
It took me a while to find this out.
AndyM (2007-08-13 21:13:04)
Be careful tom at crysis-online dot com - your suggested method seems to be server dependent. I'm currently trying to talk to a proftpd server and the size returned is -1 for all files it seems.
tom at crysis-online dot com (2007-08-09 16:12:32)
This function is way faster than those below for checking if an object is a file or folder.
<?php
function is_ftp_dir($file, $ftp_connection){
if(ftp_size($ftp_connection, $file) == '-1'){
return true; // Is directory
}else{
return false; // Is file
}
}
?>
bintjes at gmail dot com (2007-07-24 02:51:19)
I've made a recursive function to list all files from folders and subfolders using ftp_rawlist() . I used the function below from ari, parse_rawlist()
function list_all_files($conn_id, $path){
$buff = ftp_rawlist($conn_id, $path);
$res = parse_rawlist( $buff) ;
static $flist = array();
if(count($res)>0){
foreach($res as $result){
// verify if is dir , if not add to the list of files
if($result['size']== 0){
// recursively call the function if this file is a folder
list_all_files($conn_id, $path.'/'.$result['name']);
}
else{
// this is a file, add to final list
$flist[] = $result;
}
}
}
return $flist;
}
josh at servebyte dot com (2007-02-21 10:40:26)
Function Keywords: is_file is_dir
<?php
$ftp_server = 'IP OR HOSTNAME HERE';
$ftp_user_name = 'USERNAME';
$ftp_user_pass = 'PASSWORD';
// 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);
function ftp_is_dir($folder) {
global $conn_id;
if (ftp_chdir($conn_id, $folder)) {
ftp_chdir($conn_id, '..');
return true;
} else {
return false;
}
}
function xdir($path) {
global $conn_id;
$contents = ftp_nlist($conn_id, $path);
foreach($contents as $file) {
if ($file!='.'&&$file!='..') {
if (ftp_is_dir($file)) {
echo $file . ' is a folder<br>';
} else {
echo $file . ' is a file<br>';
}
}
}
}
xdir('.');
?>
can be looped through... using the function xdir if the file is a folder ;)
Returns a warning upon not being a folder... use error_reporting(0); to resolve this issue.
nigel (at) amanwithapencil (dot) com (2007-02-04 04:02:31)
Actually, this works better (suppresses the error when ftp_chdir() fails)!:
if (!@ftp_chdir($ftp_conn, $userfolder))
{
ftp_mkdir($ftp_conn, $userfolder);
ftp_chdir($ftp_conn, $userfolder);
}
nigel (at) amanwithapencil (dot) com (2007-02-03 08:26:15)
Further to kip's post, this works:
if (!ftp_chdir($conn, $folder))
{
ftp_mkdir($conn, $folder);
ftp_chdir($conn, $folder);
}
kip at friendchip dot com (2006-12-12 04:48:37)
If you're just after an ftp version of is_dir you can see if ftp_chdir returns true or false.
fx dot menard at pleasenospam dot free dot fr (2006-10-05 02:35:55)
A derived use of ftp_nlist function: determine if a given path exists and is a directory or a plain file (this is not easy with basic ftp functions). This code doesn't issue any warning or error. It makes use of ls option -d for efficiency, and -F to append a slash if the retrieved path is a directory.
<?php
// returns information on a given ftp path
// 0 if path does not exist
// 1 if path is a file
// 2 if path is a directory
function ftp_pathtype( $conn, $path )
{
$res = ftp_nlist( $conn, "-dF " . $path );
if ( isset( $res[0] ) and
strlen( $res[0] ) )
{
if ( $res[0][ strlen( $res[0] )-1 ] === "/" )
// a directory
return 2;
else
return 1;
} else
return 0;
}
?>
artiom1st at yahoo dot com (2006-08-24 07:26:31)
Sometimes, you won't be able to get folder content because of NAT or Firewall issues on your server. As a result you need to put ftp connection into a passive mode:
<?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);
// enabling passive mode
ftp_pasv( $conn_id, true );
// get contents of the current directory
$contents = ftp_nlist($conn_id, ".");
// output $contents
var_dump($contents);
?>
sksafarath at impelsys dot com (2006-08-23 04:17:42)
If we are giving second parameter as full directory path then it wil return the list of files with full path, If we want to list only files you can do as following code
// 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);
//if u want to display the files from /home/test/
//change directory point to /home/test using
ftp_chdir($conn_id, "test");
//then pass second parameter as null which will give the list of files.
// get contents of the current directory
$contents = ftp_nlist($conn_id, "");
Note: This is working for me in PHP5
robtherat at gmx dot de (2006-08-09 06:00:50)
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
nekrostar at gmx dot net (2006-08-02 04:08:36)
This function returns an array with the directory path and all files and subdirs which are in this
function ftp_searchdir($conn_id, $dir)
{
if( !@ftp_is_dir( $conn_id, $dir ) ) {
die( 'No such directory on the ftp-server' );
}
if( strrchr( $dir, '/' ) != '/' ) {
$dir = $dir.'/';
}
$dirlist[0] = $dir;
$list = ftp_nlist( $conn_id, $dir );
foreach( $list as $path ) {
$path = './'.$path;
if( $path != $dir.'.' && $path != $dir.'..') {
if( ftp_is_dir( $conn_id, $path ) ) {
$temp = ftp_searchdir( $conn_id, ($path), 1 );
$dirlist = array_merge( $dirlist, $temp );
} else {
$dirlist[] = $path;
}
}
}
ftp_chdir( $conn_id, '/../' );
return $dirlist;
}
----------------------------------------------------------------------
//Looks if a directory ($dir) is isset
//returns true or false
function ftp_is_dir( $conn_id, $dir )
{
if( @ftp_chdir( $conn_id, $dir ) ) {
ftp_chdir( $conn_id, '/../' );
return true;
} else {
return false;
}
}
mail at martinauer dot net (2006-05-14 10:27:01)
Beware:
The array will contain complete paths, not just filenames. At least in PHP 4.3.11 when I tried
ftp_nlist("www.example.com/docs/some/thing")
it dumped something like this:
[0]=>
string(41) "www.example.com/docs/some/thing/file1.htm"
[1]=>
string(41) "www.example.com/docs/some/thing/file2.htm"
[2]=>
string(41) "www.example.com/docs/some/thing/file3.htm"
[3]=>
string(41) "www.example.com/docs/some/thing/file4.htm"
[4]=>
string(41) "www.example.com/docs/some/thing/file5.htm"
franck569 at free dot fr (2006-02-24 10:32:13)
better version of turigeza on yahoo com ftp_rmdirr() function.
This return true or false if the path will be removed :
function ftp_rmdirr($handle, $path)
{
if(!@ftp_delete($handle, $path))
{
$list = @ftp_nlist($handle, $path);
if(!empty($list))
foreach($list as $value)
ftp_rmdirr($handle, $value);
}
if(@ftp_rmdir($handle, $path))
return true;
else
return false;
}
turigeza on yahoo com (2005-11-07 07:10:36)
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.
dreamsavior at gmail dot com (2005-06-29 21:19:21)
Here is the function that would checking whether the given path is directory or not, using ftp_nlist;
<?php
function is_ftp_dir($itempath, $ftp_server, $ftp_user_name='anonymous', $ftp_user_pass='') {
if (empty($ftp_user_name)) {
$ftp_user_name = "anonymous";
}
if ($itempath[0]!=="/") {
$itempath = "/".$itempath;
}
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
$contents = ftp_nlist($conn_id, "$itempath");
if (!empty($contents[0])) {
$check = str_replace($itempath, "", $contents[0]);
if (!empty($check)) {
return true;
}
}
ftp_close($conn_id);
}
// example :
if (is_ftp_dir("/pub/ftp/unknownitem", "ftp.nowhere", 'username', 'verysecret'')) {
// do something related to directory handling action
} else {
// The target item is a file ...
}
?>
ds at over dot hu (2004-12-18 08:43:32)
Another file filecollect script, with static.
<?php
function filecollect($conn_id,$dir='.') {
static $flist=array();
if ($files = ftp_nlist($cid,$dir)){
foreach ($files as $file) {
if (ftp_size($cid, $file) == "-1"){
filecollect($cid,$file);
} else $flist[] = $file;
}
}
return $flist;
}
$conn_id = ftp_connect('ftp.nowhere.com') or die("Couldn't connect to server");
if (@ftp_login($conn_id, 'anonymous', 'pass@nowhere.com')){
$filelist = filecollect($conn_id);
echo "<pre>";
print_r($filelist);
echo "</pre>";
}
?>
webmaster at torbox dot info (2004-09-26 09:16:47)
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 :-)
micksam7 at neodecoder dot com (2004-02-29 02:40:06)
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..
tobrien at florahill dot vic dot edu dot au (2003-10-13 08:27:47)
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);
?>
aRc (2003-07-14 12:46:28)
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 (2003-06-03 10:10:07)
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);
}
}
louis at ewens dot com (2003-04-29 16:03:45)
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 (2003-02-12 20:25:10)
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 (2002-03-20 19:31:01)
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
markw at altexa dot com (2000-11-13 12:33:30)
You can do a wildcard file listing with ftp_nlist, as in:
$list=ftp_nlist($conn_id, "$dir/*.txt");