PHP Glob() Function With Examples

This tutorial will help you enrich your skills in programming by learning how to use the PHP glob() function. The topic not only discusses what is glob() function but also provides various examples to enlighten you about its advantages.

As a quick overview of PHP programming, it is a language that is very ideal for web and system development. A lot of programmers were also fond of the language since it is simple and easy to comprehend.

Moreover, PHP programming offers a lot of built-in and anonymous functions which give itself an edge over other languages. To discover more of its functions, let us now discuss one of them. Let us start with…

What is glob() PHP?

The glob() function is a built-in function of PHP programming that finds and returns the directories of filenames that matches the pattern.

Syntax

To implement the PHP glob() function, you have to know first its correct syntax.

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

Specifically, the glob() function searches for all pathnames that fit the pattern using the libc glob() function’s rules, which are comparable to the rules used by ordinary shells.

Parameters

PHP glob() takes two parameters in its implementation and these parameters are the $pattern and the $flags.

Pattern

The $pattern is a required parameter of the function. This is because this parameter specifies the pattern of files or directories that the program should search.

The pattern parameter also includes some of the following characters to construct a basis or pattern.

  • The * symbol is a pattern to match any number of characters. This symbol can also match with zero or more characters.
  • You can also use the - pattern to match a single character exactly. However, the dash symbol only matches with only one character in any form.
  • Use [...] to match a single character from a group of listed characters in []. Use the ! character as the first character in the group to cancel the matching. 
  • Use the forward-slash (\)  to escape the characters listed below, unless the GLOB_NOESCAPE flag option is set.

Flags

The flags parameter is only an optional parameter and it specifies other special characters that are not required. These special characters could include the following.

The PHP glob() function returns an empty array if no files or folders match the pattern. However, if there is an error, the glob() function returns false.

PHP glob() Flags

The $flag parameter contains one or more settings that control how the glob() method behaves.

  • GLOB_MARK – Appends a slash (a backslash on Windows) to each returned directory.
  • GLOB_NOSORT – Return files in the order that they appear in the directory (no sorting). Pathnames are ordered alphabetically when this flag is not set.
  • GLOB_NOCHECK – If no files matching the search pattern were located, return the search pattern.
  • Backslashes do not quote metacharacters in GLOB NOESCAPE.
  • GLOB_BRACE – Extends a,b,c to match ‘a’,’b’,’c’.
  • GLOB_ONLYDIR – Only return directory entries that match the pattern.
  • GLOB_ERR – Errors in reading (like unreadable directories). Errors are normally overlooked.

Because the glob() function returns an array containing the matched files and directories, you can use the | character to combine flags.

Return Values

The glob () function returns an array containing the matching files or directories. But if the files or directories don’t match, it will return an empty array, or false if there was an error.

Furthermore, in some systems, distinguishing between an empty match and an error is impossible.

glob() Function In PHP Examples

This time, let us have an example program where we can apply the glob() function. The example below will let you see the very purpose of the function which is to find the files and directories on your device.

Example Program

<?php
foreach (glob("*.txt") as $file) {
    echo "$file size " . filesize($file) . "<br>";
}
?>

Program Explanation

The example shows you how the glob() function implements its parameters to provide the desired output. As you can see, the foreach function was also applied to get all (*) the files with the .txt extension.

As a result of the example program above, the files with the .txt extensions were displayed in the output.

Output

sample1.txt size 611
sample2.txt size 338
sample3.txt size 100

Kindly take note that the glob() function won’t work on remote files because the file to be looked at must be accessible through the server’s filesystem, and it is not available on some operating systems like Sun OS.

Conclusion

In conclusion, the PHP glob() function now covered every discussion needed to understand the overall function. It includes examples that enlighten programmers on the possible functions that they may apply with the glob() function.

All in all, the bottom line of this topic is to express the use and importance of the PHP glob() function. This is to help PHP programmers excel and discover more about the functionalities of the language.

Leave a Comment