PHP Reference ยท Files and I/O
PHP scandir(), glob() and opendir() functions
Reference for PHP scandir(), glob(), opendir()/readdir() and FilesystemIterator: list a folder, filter by pattern, skip dot entries, sort order and recursion.
PHP offers several ways to list a directory. scandir() returns every entry name as a sorted array, including . and ... glob() returns paths that match a wildcard pattern such as *.txt. opendir(), readdir() and closedir() read one entry at a time, which is gentler on memory for huge folders. For recursion, RecursiveDirectoryIterator from the SPL does the walking.
Pick glob() when you know the file type, scandir() when you want everything, and the handle functions when the folder may hold millions of files.
$names = array_diff(scandir($dir), [".", ".."]); $texts = glob($dir . "/*.txt");
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
scandir(string $directory, int $sorting_order = SCANDIR_SORT_ASCENDING, ?resource $context = null): array|false glob(string $pattern, int $flags = 0): array|false opendir(string $directory, ?resource $context = null): resource|false readdir(?resource $dir_handle = null): string|false closedir(?resource $dir_handle = null): void
| Name | Description |
|---|---|
$directory |
The folder to read. |
$sorting_order |
SCANDIR_SORT_ASCENDING (default), SCANDIR_SORT_DESCENDING or SCANDIR_SORT_NONE. |
$pattern |
A shell-style pattern: *, ?, [abc]. The path part is literal. |
$flags |
GLOB_ONLYDIR, GLOB_BRACE, GLOB_NOSORT, GLOB_MARK, GLOB_NOCHECK, GLOB_ERR. |
Return value: scandir() returns an array of names (not full paths), glob() an array of paths (empty if nothing matches), readdir() the next name or false.
Usage notes
scandir()includes.and... Filter them witharray_diff()as shown above.- Names versus paths.
scandir()gives bare names;glob()gives the pattern's directory plus the name, so prefix names yourself before callingis_file(). glob("*")skips dotfiles, whilescandir()lists them.- Sorting is byte-wise for
scandir(), so"B"comes before"a"and"file10"before"file2". Usenatsort()for human order. glob()returns[]when nothing matches andfalseonly on error;GLOB_BRACEis not available on every platform.- Use
!== falsewithreaddir(). A file named0is falsy. - Always
closedir()handles fromopendir(). - Failure:
scandir()on a missing directory returnsfalsewith a warning; check withis_dir()first. - Recursion needs
RecursiveDirectoryIterator(as in the example) or your own function; neitherscandir()norglob()descends. - Order is not guaranteed for
readdir()and the iterators, so sort if the order matters.
Related
is_dir()andmkdir(),file_exists(),pathinfo(),unlink()- Lessons: Directories