Using PHP's glob() Function to Search Files and Directories

This article introduces PHP's glob() function, explains its syntax and parameters, and provides multiple code examples demonstrating how to locate files, filter by patterns, search across multiple directories, and retrieve only directories, while noting performance considerations.

php Courses
php Courses
php Courses
Using PHP's glob() Function to Search Files and Directories

PHP is a widely used programming language for developing web applications, and its standard library includes powerful functions such as glob() that simplify file system tasks.

The glob() function searches for file pathnames that match a given pattern, making it easy to locate multiple files or directories.

Syntax:

glob(pattern, flags)

Parameters:

pattern

: The pattern to match, which can be a directory name, file name, or a filename containing wildcards like * or ?. flags (optional): Additional options such as searching hidden files or sorting the results.

Example 1: Find all PHP files in a specific directory

$files = glob('/path/to/directory/*.php');

This code returns an array of paths and filenames for all PHP files in the specified directory.

Example 2: Find specific files in multiple directories

$dirs = array('/path/to/directory1/', '/path/to/directory2/');
$files = array();
foreach ($dirs as $dir) {
    $files = array_merge($files, glob($dir . '*.txt'));
}

The script builds an array of two directories, iterates over each with a foreach loop, and uses glob() to collect all .txt files, merging the results with array_merge().

Example 3: Use wildcards to find multiple file types

$files = glob('/path/to/directory/*.{php,txt}', GLOB_BRACE);

This returns an array containing both PHP and TXT files; the GLOB_BRACE flag enables the brace syntax for specifying multiple extensions.

Example 4: Find all directories

$dirs = glob('/path/to/directory/*', GLOB_ONLYDIR);

The GLOB_ONLYDIR flag restricts matches to directories, returning an array of all sub‑directories.

Summary

The glob() function is a practical tool for locating files and directories, but be aware that wildcard searches can impact performance on large file sets. Mastering its usage enables more efficient file system operations in PHP projects.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Backendpattern-matchingFilesystemFile Searchglob
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.