HomeMenu
Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

scandir

Description

The scandir of Directory for PHP lists the 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

The sorted order.

ConstantDescription
SCANDIR_SORT_ASCENDINGalphabetical in ascending order. default.
SCANDIR_SORT_DESCENDINGalphabetical in descending order
SCANDIR_SORT_NONEunsorted

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] => .directory
    [3] => directory
    [4] => directory.d
)

2 · sorting_order · SCANDIR_SORT_ASCENDING

<?

$directory = "/tmp";
$sorting_order = SCANDIR_SORT_ASCENDING;

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

print_r($return);
Array
(
    [0] => .
    [1] => ..
    [2] => .directory
    [3] => directory
    [4] => directory.d
)

3 · sorting_order · SCANDIR_SORT_DESCENDING

<?

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

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

print_r($return);
Array
(
    [0] => directory.d
    [1] => directory
    [2] => .directory
    [3] => ..
    [4] => .
)

4 · sorting_order · SCANDIR_SORT_NONE

<?

$directory = "/tmp";
$sorting_order = SCANDIR_SORT_NONE;

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

print_r($return);
Array
(
    [0] => .directory
    [1] => .
    [2] => ..
    [3] => directory.d
    [4] => directory
)

5 · remove . ..

<?

$directory = "/tmp";

$return = scandir($directory);
$array =
[
    ".",
    ".."
];

$arraydiff = array_diff($return, $array);

print_r($arraydiff);
Array
(
    [2] => .directory
    [3] => directory
    [4] => directory.d
)