glob
Finds files by a name pattern.
Syntax
glob(string $pattern, int $flags = 0): array|falseParameters
| Parameter | Type | Description |
|---|---|---|
| $pattern | string | A path pattern: `*` stands for any characters. |
| $flags | int | Optional. For example, `GLOB_ONLYDIR` returns folders only. |
Return value
array|falseAn array of paths; empty if nothing is found.
Examples
Every image in a folder:
<?php
$dir = sys_get_temp_dir() . '/ref-glob';
if (!is_dir($dir)) {
mkdir($dir);
}
file_put_contents($dir . '/photo.jpg', '');
file_put_contents($dir . '/notes.txt', '');
echo implode(', ', array_map('basename', glob($dir . '/*.jpg')));Output:
photo.jpgNotes
Full paths are returned, not just names. The GLOB_BRACE flag allows several extensions at once: *.{jpg,png}.
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.