scandir
Returns the list of files and folders in a folder.
Syntax
scandir(string $directory, int $sorting_order = SCANDIR_SORT_ASCENDING): array|falseParameters
| Parameter | Type | Description |
|---|---|---|
| $directory | string | The path to the folder. |
| $sorting_order | int | Optional. 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.txtNotes
The result always has the . and .. entries — the current and the parent folder. array_diff() removes them.
See also
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.