scandir() - 列出指定路径中的文件和目录 - php 文件目录函数
scandir()
(PHP 5, PHP 7)
列出指定路径中的文件和目录
说明
scandir(string $directory[,int $sorting_order[,resource $context]]): array返回一个array,包含有$directory中的文件和目录。
参数
$directory要被浏览的目录
$sorting_order默认的排序顺序是按字母升序排列。如果使用了可选参数$sorting_order(设为 1),则排序顺序是按字母降序排列。
$context$context参数的说明见手册中的Streams API一章。
返回值
成功则返回包含有文件名的array,如果失败则返回FALSE
。如果$directory不是个目录,则返回布尔值FALSE
并生成一条E_WARNING
级的错误。
更新日志
版本 | 说明 |
---|---|
5.4.0 | $sorting_ordernow accepts constants. Any nonzero value caused descending order in previous versions. |
范例
一个简单的scandir()例子
以上例程的输出类似于:
Array ( [0] => . [1] => .. [2] => bar.php [3] => foo.txt [4] => somedir ) Array ( [0] => somedir [1] => foo.txt [2] => bar.php [3] => .. [4] => . )
Example #2scandir()在 PHP 4 中的实现
以上例程的输出类似于:
Array ( [0] => . [1] => .. [2] => bar.php [3] => foo.txt [4] => somedir ) Array ( [0] => somedir [1] => foo.txt [2] => bar.php [3] => .. [4] => . )
注释
Tip如已启用fopen 包装器,在此函数中, URL 可作为文件名。关于如何指定文件名详见fopen()。各种wapper 的不同功能请参见支持的协议和封装协议,注意其用法及其可提供的预定义变量。
参见
opendir()
打开目录句柄readdir()
从目录句柄中读取条目glob()
寻找与模式匹配的文件路径is_dir()
判断给定文件名是否是一个目录sort()
对数组排序
Easy way to get rid of the dots that scandir() picks up in Linux environments:
Here is my 2 cents. I wanted to create an array of my directory structure recursively. I wanted to easely access data in a certain directory using foreach. I came up with the following: Output Array ( [subdir1] => Array ( [0] => file1.txt [subsubdir] => Array ( [0] => file2.txt [1] => file3.txt ) ) [subdir2] => Array ( [0] => file4.txt } )
Someone wrote that array_slice could be used to quickly remove directory entries "." and "..". However, "-" is a valid entry that would come before those, so array_slice would remove the wrong entries.
Fastest way to get a list of files without dots.
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)