Skip to content
Tutorials Center
My learning

Topics

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.

PHP
$names = array_diff(scandir($dir), [".", ".."]);
$texts = glob($dir . "/*.txt");

Try it

PHP scandir() and glob()

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.

PHP Reading one entry at a time, and recursion

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

PHP
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 with array_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 calling is_file().
  • glob("*") skips dotfiles, while scandir() lists them.
  • Sorting is byte-wise for scandir(), so "B" comes before "a" and "file10" before "file2". Use natsort() for human order.
  • glob() returns [] when nothing matches and false only on error; GLOB_BRACE is not available on every platform.
  • Use !== false with readdir(). A file named 0 is falsy.
  • Always closedir() handles from opendir().
  • Failure: scandir() on a missing directory returns false with a warning; check with is_dir() first.
  • Recursion needs RecursiveDirectoryIterator (as in the example) or your own function; neither scandir() nor glob() descends.
  • Order is not guaranteed for readdir() and the iterators, so sort if the order matters.