scandir

List files and directories inside the specified path

Syntax

scandir(string $directory, int $sorting_order = SCANDIR_SORT_ASCENDING, ?resource $context = null): array|false

Parameters

directory

The directory that will be scanned.

sorting_order

By default, the sorted order is alphabetical in ascending order. If the optional sorting_order is set to SCANDIR_SORT_DESCENDING, then the sort order is alphabetical in descending order. If it is set to SCANDIR_SORT_NONE then the result is unsorted.

context

For a description of the context parameter, refer to the streams section of the manual.

Return

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.

Examples

1 · directory

<?

$directory = '/tmp';

$return = scandir($directory);

print_r($return);

?>
Array
(
    [0] => .
    [1] => ..
    [2] => dir1
    [3] => dir2
)

2 · sorting_order

<?

$directory = '/tmp';
$sorting_order = SCANDIR_SORT_DESCENDING;

$return = scandir($directory, $sorting_order);

print_r($return);

?>
Array
(
    [0] => dir2
    [1] => dir1
    [2] => ..
    [3] => .
)

3 · array_diff

<?

$directory = '/tmp';

$array1 = scandir($directory);
$array2 = array('.', '..');

$return = array_diff($array1, $array2);

print_r($return);

?>
Array
(
    [2] => dir1
    [3] => dir2
)

chdir

chroot

closedir

dir

getcwd

opendir

readdir

rewinddir

PHP

Home Menu