Master PHP’s glob() Function: Find Files & Directories Efficiently

Learn how to use PHP’s powerful glob() function to locate files and directories with pattern matching, explore its syntax, optional flags, and practical examples ranging from simple file listings to recursive directory searches and directory-only queries.

php Courses
php Courses
php Courses
Master PHP’s glob() Function: Find Files & Directories Efficiently

PHP's glob() function is a powerful file operation utility that finds files or directories based on a pattern and returns their paths.

Syntax of glob()

glob(pattern, flags)

Here, pattern is the matching pattern string, which can be a wildcard expression such as *.txt (matches files ending with .txt) or a specific file path. flags is optional and controls the function's behavior.

Usage of glob()

Example 1: List all files in a directory

$files = glob('/var/www/html/*');
foreach ($files as $file) {
    echo $file . '<br>';
}

This code searches the directory /var/www/html and prints the paths of all files found. It uses a direct directory path without a wildcard.

Example 2: Match files with a specific extension

$files = glob('/var/www/html/*.txt');
foreach ($files as $file) {
    echo $file . '<br>';
}

This code searches /var/www/html for all files ending with .txt using the wildcard *.txt and prints their paths.

Example 3: Recursively find all files in a directory

function getAllFiles($dir) {
    $files = glob($dir . '/*');
    foreach ($files as $file) {
        if (is_dir($file)) {
            getAllFiles($file);
        } else {
            echo $file . '<br>';
        }
    }
}
getAllFiles('/var/www/html');

This recursive function retrieves all files under the specified directory, calling getAllFiles() for each subdirectory.

Example 4: List only directories

$directories = glob('/var/www/html/*', GLOB_ONLYDIR);
foreach ($directories as $directory) {
    echo $directory . '<br>';
}

This code searches /var/www/html for subdirectories only, using the GLOB_ONLYDIR flag to match directories.

The glob() function in PHP is a convenient and efficient tool for locating files or directories with flexible pattern matching and various options to suit different needs.

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.

BackendPHPFile Searchglobdirectory
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.