array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'zh', ), 'this' => array ( 0 => 'function.scandir.php', 1 => 'scandir', ), 'up' => array ( 0 => 'ref.dir.php', 1 => '目录函数函数', ), 'prev' => array ( 0 => 'function.rewinddir.php', 1 => 'rewinddir', ), 'next' => array ( 0 => 'book.fileinfo.php', 1 => 'Fileinfo', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'zh', 'path' => 'reference/dir/functions/scandir.xml', ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); ?>
(PHP 5, PHP 7, PHP 8)
scandir — 列出指定路径中的文件和目录
$directory
, int $sorting_order
= SCANDIR_SORT_ASCENDING
, ?resource $context
= null
): array|false
返回 array,包含有
directory
中的文件和目录。
directory
要被浏览的目录
sorting_order
默认的排序顺序是按字母升序排列。如果使用了可选参数
sorting_order
(设为
1),则排序顺序是按字母降序排列。
context
context
参数的说明见手册中的
Streams API 一章。
版本 | 说明 |
---|---|
8.0.0 |
现在 context 允许为 null。
|
示例 #1 简单的 scandir() 示例
<?php
$dir = '/tmp';
$files1 = scandir($dir);
$files2 = scandir($dir, SCANDIR_SORT_DESCENDING);
print_r($files1);
print_r($files2);
?>
以上示例的输出类似于:
Array ( [0] => . [1] => .. [2] => bar.php [3] => foo.txt [4] => somedir ) Array ( [0] => somedir [1] => foo.txt [2] => bar.php [3] => .. [4] => . )
如已启用fopen 包装器,在此函数中, URL 可作为文件名。关于如何指定文件名详见 fopen()。各种 wapper 的不同功能请参见 支持的协议和封装协议,注意其用法及其可提供的预定义变量。