PHP glob

Created with Sketch.

PHP glob

Introduction to the PHP glob() function

The glob() function finds pathnames that match a pattern.

Here’s the syntax of the glob() function:

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

Code language: PHP (php)

The glob() function takes two parameters:

The $pattern is the pattern to match. To construct a pattern, you use the following special characters:

  • Use * to match zero or more characters
  • Use - to match exactly one character
  • Use [] to match one character from a group of characters listed in the []. To negate the matching, you use the ! character as the first character in the group.
  • Use \ to escape the following characters, except when the GLOB_NOESCAPE flag is set.

The $flag is one or more options that determine the behavior of the glob() function.

For example, if the $flag is GLOB_MARK, the glob() function adds a slash (/) or backslash (\) to each matching directory. For a complete list of valid flags, check the glob() function documentation.

To combine flags, you use the | character. For example: GLOB_ONLYDIR|GLOB_NOSORT.

The glob() function returns an array that contains the matched files/directories.

If no files or directories match the pattern, the glob() function returns an empty array.

If an error occurs, the glob() function returns an false.

PHP glob() function examples

Let’s take some examples of using the PHP glob() function.

1) Using the PHP glob() function to list all files in a directory

The following example uses the glob() funtion to list all *.php file in the src directory:

<?php

$path = ‘src/*.php’;

$filenames = glob($path);

foreach ($filenames as $filename) {
echo $filename . ‘<br>’;
}

Code language: PHP (php)

In this example, the pattern *.php matches any file whose extension is php.

2) Using the PHP glob() function to calculate get the total size of files

To get the total size of files in a directory which match a pattern, you use these steps:

  • First, find the matching files using the glob() function.
  • Second, get the size of each file by passing the result of the glob() function to the array_map() function.
  • Third, get the total size by passing the result of the array_map() to the array_sum() function.

The following code illustrates how to use the glob(), array_map(), and array_sum() function to find the total size of the *.php files in the src directory:

echo array_sum(array_map('filesize', glob('./src/*.php')));

Code language: PHP (php)

3) Using the PHP glob() function to include the dotfiles

The glob(‘*’) ignores hidden files by default. This means that it doesn’t return the file whose name starts with a dot e.g., (.gitignore), which are known as dotfiles.

If you want to match the dotfiles, you can use the {.[!.],}* as the pattern with the GLOB_BRACE flag. This pattern excludes the directories . and ..:

$filenames = glob('{.[!.],}*', GLOB_BRACE);
foreach ($filenames as $filename) {
echo $filename . '<br>';
}

Code language: PHP (php)

Summary

  • Use the PHP glob() function to get a list of files and directories that match a pattern.

Leave a Reply

Your email address will not be published. Required fields are marked *