Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

glob

Finds files by a name pattern.

Syntax

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

Parameters

ParameterTypeDescription
$patternstringA path pattern: `*` stands for any characters.
$flagsintOptional. 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.jpg

Notes

Full paths are returned, not just names. The GLOB_BRACE flag allows several extensions at once: *.{jpg,png}.

See also

  • scandir — Returns the list of files and folders in a folder.
  • basename — Returns the last part of a path — the file name.

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.

Thanks for reading!

Donate to the site — we work hard for you.