php

Use of glob() function in PHP

glob() function is a built-in PHP function that is used to search the specific files or folders based on the pattern. It returns the file and folder names in an array that matches the pattern. How this function can be used to search the particular files or folders is shown in this tutorial.

Syntax:

The syntax of the glob() function is given below. This function can take two arguments. The first argument takes the pattern value that will be used to search the file and folder. The second argument is optional that is used to generate the output in different ways. The common symbols that are used to define the pattern and the different types of flags that can be used in the second argument of this function are described below.

array glob ( string $pattern [, int $flags = 0 ] )

Mostly used symbols in the pattern

Pattern Purpose
? It is used to match exactly one character (any).
* It is used to match zero or more characters.
\ It is used to escape the characters when the GLOB_NOESCAPE flag is used.
[…] It is used to match the range of characters.

Flag values

The following flag values can be used in the optional argument of the glob() function.

Value Purpose
GLOB_MARK It adds a slash with each returned item.
GLOB_NOSORT It returns unsorted files that appear in the directory.
GLOB_NOCHECK It returns the search pattern if no match is found.
GLOB_NOESCAPE It uses backslashes and doesn’t quote metacharacters.
GLOB_BRACE It expands the characters from a group to match.
GLOB_ONLYDIR It returns the directory list that only matched with the pattern.
GLOB_ERR It is used to stop when the error occurs.

Example 1: Read all PHP files using the ‘*’ symbol

The following example shows the way to search all PHP files of the current location using the ‘*.php’ pattern. Create a PHP file with the following script.

The pattern will search any filename with the extension PHP. The return value of the function is an array that will be printed as output.

<?php
//Print the list of text files of the current directory
print_r(glob("*.php"));
?>

Output:

The following output will appear after running the script from the server. It shows that five PHP files exist in the current location.

Example 2: Read specific text files using the ‘?’ symbol

The following example will search all text files that contain a filename of five characters. Create a PHP file with the following script.

The ‘?????.txt’ pattern is used to search the text file with the five-characters filename. The output of the glob() function is an array that is stored in the variable, $files. The values of this variable are printed by using the foreach loop.

<?php
   //Read specific text filenames of the current location
   $files = glob("?????.txt");
     //Print the file names
   foreach ($files as $file) {
      echo "<b>" . $file. "</b> <br />";
   }
?>

Output:

The following output will appear after running the script from the server. It shows that two text files exist in the current location according to the pattern.

Example 3: Read all files of the current location using the loop

The following example will search all types of files from the current location and print the filenames in each line by using the loop. ‘*.*’ pattern is used in the glob() function to search any file of any type. The returned value of the function is stored in the array, $files. Then, the total number of files is counted from the searched result. foreach loop is used to print the values of the array in each line.

<?php
   //Read all filenames of the current location
   $files = glob("*.*");
   //Count the total number of files
   $count = count($files);
   echo "Total files = <b> $count </b><br /><br />";
   echo "The files are : <br />";
   //Print the file names
   foreach ($files as $file) {
      echo "<b>" . $file. "</b> <br />";
   }
?>

Output:

The following output will appear after running the script from the server. It shows that six files exist in the current location.

Example 4: Search file that starts with the specific character

The following example will search the PHP file that starts with the character ‘g’. Create a PHP file with the following script.

‘g*.php’ is used as the pattern for searching the files. Like the previous examples, the returned value of the glob() function is stored in an array that is printed later using a foreach loop.

<?php
    //Search file start with 'g'
    $files = glob("g*.php");
    //Print the files
    foreach ($files as $file) {
         echo $file. "<br />";
   }
?>

Output:

The following output will appear after running the script from the server. It shows that four PHP files exist in the current location where the files start with the character ‘g’.

Example 5: Read all files and folders

The pattern used in all previous examples searched only the files from the current location. The following example shows the way to search all files and folders of the current location. Create a PHP file with the following script.

‘*’ is used as a pattern in the glob() function to search all files and folders. The returned values of the function are stored in an array that is printed later.

<?php
    //Read all files and folders of the current location
    $files = glob("*");
    //Print the files and folders
    foreach ($files as $file) {
        echo $file. "<br />";
   }
?>

Output:

The following output will appear after running the script from the server. It shows that five PHP files, three text files, and two folders exist in the current location.

Conclusion

The methods of searching any file or folder are shown in this tutorial using the glob() function of PHP. The file can be searched based on the extension, the starting character, or by specifying the total number of characters. Hopefully, the use of the glob() function in PHP will be clearer and easier for the readers after practicing the examples of this tutorial.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.