PHP scandir() Function

The scandir() function returns an array of files and directories of the specified directory.

array scandir ( string $directory [, int $sorting_order = SCANDIR_SORT_ASCENDING [, resource $context ]] )

Returns an array of files and directories from the directory.

Example -

Result -

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] => .
)

ParameterDescription
directoryRequired. Specifies the directory to be scanned
sorting_orderOptional. 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
contextOptional. Specifies the context of the directory handle. Context is a set of options that can modify the behavior of a stream