Content · Programming · PHP

PHP glob() Function: Usage and Examples

Finds paths matching a shell-style glob pattern.

1 views Published Oct 17, 2026 Updated Oct 17, 2026

glob() Function is one of the PHP operations used in filesystem and stream workflows. Finds paths matching a shell-style glob pattern. The examples below show what it does and how to handle its result.

What Is glob() Function?


Finds paths matching a shell-style glob pattern.

Its syntax is:

glob(string $pattern, int $flags = 0)

The parameters are:

  • string $pattern — A matching pattern; glob() uses filename wildcards, while Phar::buildFromDirectory() accepts a regular expression.
  • int $flags = 0 — Option constants that alter how the operation behaves.

Return value: The declared return type is array|false. glob() supports filename wildcards, not full regular expressions, and can return false on error.

Important: glob() supports filename wildcards, not full regular expressions, and can return false on error.

Example 1: Basic Usage


The basic example focuses on the behavior of glob() Function: finds paths matching a shell-style glob pattern. Inputs are defined locally or shown as application-controlled paths.

<?php

$phpFiles = glob(__DIR__ . "/*.php");
if ($phpFiles !== false) { foreach ($phpFiles as $filePath) { echo basename($filePath) . "\n"; } }

The displayed value, file content or return status depends on the input and surrounding environment. Results involving local paths, identifiers or generated files are not fixed values.

Example 2: A Different Use Case


This second example illustrates another input or execution condition for glob() Function. Notice the different checks, arguments, or output handling.

<?php

$images = glob(__DIR__ . "/images/*.{jpg,png}", GLOB_BRACE);
if ($images !== false) { echo count($images); }

This variation matters because glob() supports filename wildcards, not full regular expressions, and can return false on error.

Practical Example: Using the Operation in an Application


The following example demonstrates how glob() Function fits into a more complete file or archive workflow, including inputs and appropriate checks.

<?php

$candidateFiles = glob(__DIR__ . "/reports/*.csv", GLOB_NOSORT);
if ($candidateFiles === false) { throw new RuntimeException("Pattern could not be read"); }
foreach ($candidateFiles as $path) { echo basename($path) . "\n"; }

The operation shown is an illustrative script. Adapt the filenames, required permissions, and error handling to your own controlled application environment before running it.

Important Behavior and Edge Cases


  • Function behavior: Finds paths matching a shell-style glob pattern.
  • Limitation: glob() supports filename wildcards, not full regular expressions, and can return false on error.

Comparison With Related Functions


Function or MethodDifference
glob() FunctionFinds paths matching a shell-style glob pattern.
scandir()Enumerates a specific directory; glob() applies filename wildcard patterns.

Use glob() Function for the specific behavior described above. The related operation, scandir(), serves a different purpose; do not substitute it unless that different behavior is intended.

Common Beginner Mistakes


MistakeCorrect Approach
Assuming this operation has the same behavior on every file or environment.glob() supports filename wildcards, not full regular expressions, and can return false on error. Check prerequisites and return values before using the result.
Treating scandir() as an interchangeable replacement for glob() Function.Enumerates a specific directory; glob() applies filename wildcard patterns.

Key Takeaways


  • Finds paths matching a shell-style glob pattern.
  • glob() supports filename wildcards, not full regular expressions, and can return false on error.
  • Related operation: scandir() — Enumerates a specific directory; glob() applies filename wildcard patterns.

glob() Function is useful when its specific behavior matches your application requirement. Keep paths controlled, check failures, and handle environment-dependent operations deliberately.

Share this resourceSend this page to someone who may find it useful.