scandir
(PHP 5, PHP 7, PHP 8)
List files and directories inside the specified path
Description
Returns an array of files and directories from the directory.
Parameters
directoryThe directory that will be scanned.
sorting_orderBy default, the sorted order is alphabetical in ascending order. If the optional
sorting_orderis set toSCANDIR_SORT_DESCENDING, then the sort order is alphabetical in descending order. If it is set toSCANDIR_SORT_NONEthen the result is unsorted.contextFor a description of the
contextparameter, refer to the streams section of the manual.
Return Values
Returns an array of filenames on success, or false on failure. If directory is not a directory, then boolean false is returned, and an error of level E_WARNING is generated.
Changelog
| Version | Description |
|---|---|
| 8.0.0 | context is now nullable. |
Examples
A simple scandir() example
<?php
$dir = '/tmp';
$files1 = scandir($dir);
$files2 = scandir($dir, SCANDIR_SORT_DESCENDING);
print_r($files1);
print_r($files2);
?>The above example will output something similar to:
Array
(
[0] => .
[1] => ..
[2] => bar.php
[3] => foo.txt
[4] => somedir
)
Array
(
[0] => somedir
[1] => foo.txt
[2] => bar.php
[3] => ..
[4] => .
)