目录

PHP scandir() 函数

❮ PHP 目录参考

示例

列出 images 目录中的文件和目录:

<?php
$dir = "/images/";

// Sort in ascending order - this is default
$a = scandir($dir);

// Sort in descending order
$b = scandir($dir,1);

print_r($a);
print_r($b);
?>

结果:

Array
(
[0] => .
[1] => ..
[2] => cat.gif
[3] => dog.gif
[4] => horse.gif
[5] => myimages
)
Array
(
[0] => myimages
[1] => horse.gif
[2] => dog.gif
[3] => cat.gif
[4] => ..
[5] => .
)


定义和用法

scandir() 函数返回指定目录的文件和目录的数组。


语法

scandir( directory, order, context)

参数值

Parameter Description
directory Required. Specifies the directory to be scanned
order Optional. Specifies the sorting order. Default sort order is alphabetical in ascending order (0). Set to SCANDIR_SORT_DESCENDING or 1 to sort in alphabetical descending order, or SCANDIR_SORT_NONE to return the result unsorted 
context Optional. Specifies the context of the directory handle. Context is a set of options that can modify the behavior of a stream


技术细节

返回值: 成功时为文件和目录数组,失败时为 FALSE。如果目录不是目录,则抛出 E_WARNING
PHP 版本: 5.0+
PHP 变更日志: PHP 5.4:命令添加了常量

❮ PHP 目录参考