PHP glob() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, glob() finds file and folder names that match a pattern, such as every .txt file in a folder. It works like the wildcards you may know from the command line.

You give it a pattern with characters like * and ?, and it returns an array of the matching paths, sorted alphabetically. The array is empty when nothing matches.

Syntax

Call glob() with the values you need. Start with this line.

PHP
$files = glob('uploads/*.txt');
CallWhat it does
glob('*.txt')Returns every .txt file in the current folder.
glob('data/file?.csv')Matches names like file1.csv or fileA.csv, where ? stands for one character.
glob('images/*', GLOB_ONLYDIR)Returns only the sub folders inside the images folder.

The * matches any number of characters and ? matches exactly one. A pattern does not look inside sub folders. To search deeper, write each level in the pattern, such as glob('*/*.txt').

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

Parameters

ParameterDescription
patternRequired. The pattern to match. It can include a folder path, * for any characters and ? for one character.
flagsOptional. One or more flags such as GLOB_BRACE (allow {a,b} choices), GLOB_ONLYDIR (folders only), GLOB_MARK (add a slash to folders) or GLOB_NOSORT (do not sort). The default is 0.

Return Value

Return ValueDescription
arrayAn array of matching file and folder paths. It is an empty array when nothing matches.
falseReturned only when an error occurs.

PHP Examples

Example 1 — List All .txt Files in a Folder

Create a temporary folder with a few files, then use glob() to pick out only the text files.

PHP
<?php
$dir = sys_get_temp_dir() . '/glob_demo';
mkdir($dir);

foreach (['b.txt', 'a.txt', 'c.php'] as $name) {
    file_put_contents($dir . '/' . $name, 'x');
}

foreach (glob($dir . '/*.txt') as $file) {
    echo basename($file), PHP_EOL;
}

array_map('unlink', glob($dir . '/*'));
rmdir($dir);

How It Works

  1. A temporary folder is created and three small files are placed in it.
  2. The pattern *. txt matches any file name that ends with .txt, so c.php is skipped.
  3. glob() returns full paths sorted alphabetically, so a. txt comes before b.txt.
  4. basename() trims each path to the file name, and the cleanup code removes the files and the folder.

Example 2 — Handle No Matching Files

When nothing matches the pattern, glob() returns an empty array. Check for that before you loop.

PHP
<?php
$dir = sys_get_temp_dir() . '/glob_empty';
mkdir($dir);
file_put_contents($dir . '/note.txt', 'x');

$files = glob($dir . '/*.pdf');

if ($files === false) {
    echo "An error happened", PHP_EOL;
} elseif (count($files) === 0) {
    echo "No PDF files found", PHP_EOL;
}

unlink($dir . '/note.txt');
rmdir($dir);

How It Works

  1. The folder contains only note. txt, so the pattern *.pdf has nothing to match.
  2. glob() returns an empty array, not false.
  3. The first check for false is skipped because false means a real error.
  4. count($files) === 0 is true, so the friendly message is printed.

Example 3 — Find Only Sub Folders with GLOB_ONLYDIR

Add the GLOB_ONLYDIR flag to list folders and ignore regular files.

PHP
<?php
$dir = sys_get_temp_dir() . '/glob_dirs';
mkdir($dir);
mkdir($dir . '/images');
mkdir($dir . '/css');
file_put_contents($dir . '/index.php', 'x');

foreach (glob($dir . '/*', GLOB_ONLYDIR) as $folder) {
    echo basename($folder), PHP_EOL;
}

rmdir($dir . '/images');
rmdir($dir . '/css');
unlink($dir . '/index.php');
rmdir($dir);

How It Works

  1. The temporary folder holds two sub folders and one regular file.
  2. The pattern * would match all three, but GLOB_ONLYDIR keeps only folders.
  3. The results are sorted, so css comes before images and index. php is left out.
  4. The cleanup lines remove everything that was created.

Frequently Asked Questions

It returns an array of file and folder paths that match a pattern, such as all .jpg files in a folder.
It returns an empty array. It returns false only when an error occurs, so check with count() or empty().
Yes. Use the GLOB_BRACE flag with a pattern like '*.{jpg,png}'. If your system does not support GLOB_BRACE, call glob() once for each extension and merge the results.
No. It only matches the levels written in the pattern. For deep searches, use a recursive loop with RecursiveDirectoryIterator.
Yes, alphabetically by default. Add the GLOB_NOSORT flag if you do not need sorting and want a little more speed.

Next: is_dir()

is_dir() tells you whether a path points to an existing directory (folder). It returns tru.

is_dir() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful