ftp_nlist() - ftp函数
ftp_nlist()
(PHP 4, PHP 5, PHP 7)
返回给定目录的文件列表
说明
ftp_nlist(resource $ftp_stream,string $directory): array参数
$ftp_streamFTP 连接资源。
$directory指定要列表的目录。本参数接受带参数的形式,例如: ftp_nlist($conn_id,"-la /you/dir");注意此参数不对传入值做处理,在目录或者文件名包括空格或特殊的情况下,可能会存在问题。
返回值
如果成功则返回给定目录下的文件名组成的数组,否则返回FALSE
。
范例
ftp_nlist()例子
以上例程的输出类似于:
array(3) { [0]=> string(11) "public_html" [1]=> string(10) "public_ftp" [2]=> string(3) "www"
参见
ftp_rawlist()
返回指定目录下文件的详细列表
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);
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...
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.
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. Greets, Jacob
So I wrote a simple function using ftp_nlist() to recursively list only files in a specified directory. /** * Returns a list of files in the given directory, excluding directories. * * @param resource $ftp_stream * The link identifier of the FTP connection. * @param string $directory * The directory to be listed. * Note that this parameter isn't escaped so there may be some issues with filenames containing spaces and other characters. * @param bool $recursive * (optional) If set to TRUE, the issued command will be done recursively. * * @return array * Returns an array of filenames from the specified directory. */ function ftp_files_list($ftp_stream, $directory, $recursive = false) { $files = array(); $list = ftp_nlist($ftp_stream, $directory); if (is_array($list)) { // Strip away dot directories. $list = array_slice($list, 2); foreach ($list as $filename) { $path = $directory . '/' . $filename; // If size equals -1 it's a directory. if (ftp_size($ftp_stream, $path) === -1) { if ($recursive) { $files = array_merge($files, ftp_files_list($ftp_stream, $path, $recursive)); } } else { // Strip away root directory path to ensure all file paths are relative. $files[] = substr($path, strpos($path, '/') + 1); } } } return $files; }
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, "");
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"
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; } }
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; } }
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.
Here is the function that would checking whether the given path is directory or not, using ftp_nlist;
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. I hope it's usefull for anyone Robert
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; }
To use custom LIST - flags, for example LIST -aF ../*/ (listing protected pub dirs) You can use Took me a while to clever that one out :-)
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 Dateien in ".$dir." :
"; foreach($files as $file) { print $file."
"; } print "\tOrdner in ".$dir." :
"; foreach ($folders as $folder) { print "\t".$folder."
"; ftp_chdir($this->conn_id, $dir); $this->files($folder); } }
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
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); }
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; }
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
Another file filecollect script, with static.
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)