glob()

glob() — Find pathnames matching a pattern.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

The glob() function searches for all the pathnames matching pattern according to the rules used by the libc glob() function, which is similar to the rules used by common shells.

Signature

glob(string $pattern, int $flags = 0): array|false

Parameters

ParameterTypeWhat it takes
$patternstringThe pattern. No tilde expansion or parameter substitution is done.
$flagsintAny of the GLOB_* constants.

Return value

Returns an array containing the matched files/directories, an empty array if no file matched or false on error. Unless GLOB_NOSORT was used, the names will be sorted alphanumerically.

Example

A runnable snippet:

foreach (glob("*.txt") as $filename) { echo "$filename size " . filesize($filename) . "\n"; }

Output:

funclist.txt size 44686 funcsummary.txt size 267625 quickref.txt size 137820

See also

opendir(), readdir(), closedir(), fnmatch()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page glob() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.