Backend Development 4 min read

Using PHP glob() Function for File and Directory Searching

This article explains the PHP glob() function, detailing its syntax, parameters, and multiple usage examples—including listing all files, filtering by extension, recursive file discovery, and directory-only searches—while providing complete code snippets for each scenario.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP glob() Function for File and Directory Searching

The PHP glob() function is used to find files or directories; it is a powerful file‑handling function that returns paths matching a specified pattern.

Syntax of glob() function:

<code>glob(pattern, flags)</code>

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

Usage of glob() function

Example 1: Find all files in a specified directory

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

This code searches the directory /var/www/html for all files and prints their paths. The example uses a direct directory path without a wildcard.

Example 2: Match files with a specific extension

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

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

Example 3: Recursively find all files in a directory

<code>function getAllFiles($dir) {
    $files = glob($dir . '/*');
    foreach ($files as $file) {
        if (is_dir($file)) {
            getAllFiles($file);
        } else {
            echo $file . '&lt;br&gt;';
        }
    }
}

getAllFiles('/var/www/html');</code>

This recursive function retrieves all files and sub‑directories under the given directory, calling getAllFiles() on each sub‑directory to continue the search.

Example 4: Find all directories in a specified path

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

This code searches /var/www/html for all sub‑directories and prints their paths. By setting the flags parameter to GLOB_ONLYDIR , only directories are matched.

The glob() function in PHP is a versatile tool for locating files or directories based on patterns, offering flexibility for various file‑handling and directory‑traversal tasks.

backendglobfile handlingdirectory traversal
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

login 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.