PHP scandir () function
Examples
Images are listed in the directory of files and directories:
<?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);
?>
$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);
?>
result:
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] => .
)
(
[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] => .
)
Definition and Usage
scandir () function returns an array of the specified directory files and directories.
grammar
scandir( directory,sorting_order,context );
参数 | 描述 |
---|---|
directory | 必需。规定要扫描的目录。 |
sorting_order | 可选。规定排列顺序。默认是 0,表示按字母升序排列。如果设置为 SCANDIR_SORT_DESCENDING 或者 1,则表示按字母降序排列。如果设置为 SCANDIR_SORT_NONE,则返回未排列的结果。 |
context | 可选。规定目录句柄的环境。context 是可修改目录流的行为的一套选项。 |
technical details
return value: | Successful return an array of files and directories. It failed to return FALSE. If the directory is not a directory, then throws E_WARNING level error. |
---|---|
PHP version: | 5.0+ |
PHP Update Log: | PHP 5.4: new sorting_order constant. |
PHP Directory Reference