Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

scandir

Returns the list of files and folders in a folder.

Syntax

scandir(string $directory, int $sorting_order = SCANDIR_SORT_ASCENDING): array|false

Parameters

ParameterTypeDescription
$directorystringThe path to the folder.
$sorting_orderintOptional. The sort order.

Return value

array|falseAn array of names, or `false` on failure.

Examples

The content of a folder without the service entries:

<?php
 
$dir = sys_get_temp_dir() . '/ref-scandir';
if (!is_dir($dir)) {
    mkdir($dir);
}
file_put_contents($dir . '/a.txt', '');
file_put_contents($dir . '/b.txt', '');
 
echo implode(', ', array_diff(scandir($dir), ['.', '..']));

Output:

a.txt, b.txt

Notes

The result always has the . and .. entries — the current and the parent folder. array_diff() removes them.

See also

  • glob — Finds files by a name pattern.
  • is_dir — Checks whether a path is a folder.

Related recipes

In the tutorial

  • Folders and paths — Working with folders in PHP — creating them, listing files, searching by a mask, parts of a path and file information.

Thanks for reading!

Donate to the site — we work hard for you.