Read Files and Directories in a Folder with PHP Glob
To read all the files in a particular directory using PHP, we can use the glob() function. Moreover, this function allows you to read any pathname file or subdirectory in a specific directory where you are searching for files.
Definition:
The PHP glob()
function returns an array of filenames or directories that match a specified pattern (the pattern you provide as the first argument). Furthermore, the glob()
function returns an array of matched filenames or directory names, an empty array if it does not match any files or directories, and FALSE if it encounters any errors.
Syntax of the glob()
Function
Here’s how you can use the glob()
function in PHP:
glob ( string $pattern [, int $flags = 0 ] ) : array
Description
$pattern
: This is the pattern you’re searching for. It’s a required string.
$flags
: This is optional. It’s an integer that changes how the function behaves.
Using the glob()
Function
Let’s break down how to use glob()
. Firstly, you start with a string pattern, which acts as a search filter for the files and directories you want to find.
How to use the PHP glob() function
The first parameter is a string which is treated as a pattern to search pathnames (files/directories).
Examples of glob() function by patterns
Let’s look at some examples to see how this works!
Example 1: To Search Any File or Directory
$search_results = glob('*');
print_r($search_results);
Output:
Array
(
[0] => folder-one
[1] => folder-two
[2] => index.php
[3] => php-file-one.php
[4] => php-file-two.php
[5] => techbriefers-file.txt
[6] => techbriefers-folder
[7] => test-file-one.txt
[8] => test-file-two.txt
)
In this example, using *
means “give me everything” in the directory. You can see that it returns folders and files!
Example 2: To Search Only PHP Files
$search_results = glob('*.php');
print_r($search_results);
Output:
Array
(
[0] => index.php
[1] => php-file-one.php
[2] => php-file-two.php
)
Here, we’re looking specifically for files that end with .php
. This is super useful when you only want to see your PHP scripts!
Example 3: To Search Only Text Files
$search_results = glob('*.txt');
print_r($search_results);
Output:
Array
(
[0] => techbriefers-file.txt
[1] => test-file-one.txt
[2] => test-file-two.txt
)
This time, we’re only interested in text files. You can see the results show just those!
More Specific Searches
Also, You can search for files that meet certain criteria:
Example 4: To Search Files Starting with ‘a’
$search_results = glob('a*');
print_r($search_results);
Output:
Array
(
)
In this case, there are no files or directories that start with ‘a’. So, we get an empty array!
Example 5: To Search Files Starting with ‘tech’
$search_results = glob('tech*');
print_r($search_results);
Output:
Array
(
[0] => techbriefers-file.txt
[1] => techbriefers-folder
)
Here, we found two items that start with ‘tech’. Pretty neat!
Example 6: To Search for Files Containing ‘brie’
$search_results = glob('*brie*');
print_r($search_results);
Output:
Array
(
[0] => techbriefers-file.txt
[1] => techbriefers-folder
)
This one gives us items that have ‘brie’ anywhere in their names!
Example 6: To search any file or directory that starts with ‘tech’ and end with ‘folder’
$search_results = glob('tech*folder');
print_r($search_results);
/*output
Array
(
[1] => techbriefers-folder
)
*/
The second parameter is an integer which should be a valid flag.
Using Flags with PHP glob()
Now, let’s talk about the second parameter: flags. Flags are special options that change how the glob()
function behaves. Here are some valid flags you can use:
- GLOB_MARK: This adds a slash (/) at the end of each directory name found.
- GLOB_NOSORT: This returns files in the order they appear in the directory, instead of sorting them alphabetically.
- GLOB_NOCHECK: If no files are found, it returns the pattern you searched for instead of an empty array.
- GLOB_NOESCAPE: This means that backslashes won’t escape characters.
- GLOB_BRACE: Expands patterns like
{a,b,c}
to match ‘a’, ‘b’, or ‘c’. - GLOB_ONLYDIR: This returns only directory entries that match the pattern.
- GLOB_ERR: Stops on read errors (like unreadable directories).
Examples of glob() function by flagsExamples of Using Flags
Example 7: To Add Slashes at the End of Directories
$search_results = glob('*', GLOB_MARK);
print_r($search_results);
/*output
Array
(
[0] => folder-one/
[1] => folder-two/
[2] => index.php
[3] => php-file-one.php
[4] => php-file-two.php
[5] => techbriefers-file.txt
[6] => techbriefers-folder/
[7] => test-file-one.txt
[8] => test-file-two.txt
)
*/
With the GLOB_MARK
flag, directories now have a trailing slash to show they are folders!
Example 8: To See an Unsorted List of Files and Directories
$search_results = glob('*', GLOB_NOSORT);
print_r($search_results);
/*output
Array
(
[0] => test-file-one.txt
[1] => index.php
[2] => techbriefers-file.txt
[3] => php-file-one.php
[4] => php-file-two.php
[5] => test-file-two.txt
[6] => folder-two
[7] => folder-one
[8] => techbriefers-folder
)
*/
Using GLOB_NOSORT
returns files in the order they were found, rather than sorting them alphabetically. This can be helpful if you want to keep the original order!
Example 9: If a File/Folder Is Not Found Returns Pattern
$search_results = glob('new*', GLOB_NOCHECK);
print_r($search_results);
/*output
Array
(
[0] => new*
)
*/
In this case, since we didn’t find anything matching ‘new*’, we get back the pattern instead of an empty array. This is different from the previous examples where no matches resulted in an empty array.
A clear difference can be seen if you compare Example-9 and Example-4. In Eg-4 results are not found hence empty array is returned.
Example 10: To Search Only Directories
$search_results = glob('*', GLOB_ONLYDIR);
print_r($search_results);
/*output
Array
(
[0] => folder-one
[1] => folder-two
[2] => techbriefers-folder
)
*/
With GLOB_ONLYDIR
, we only get back the directories, ignoring any files.
Searching in a Specific Sub-Folder
Example 11: To Search Files and Folders in ‘techbriefers-folder’
$search_results = glob('techbriefers-folder/*');
print_r($search_results);
/*output
Array
(
[0] => techbriefers-folder/sub-file-one.txt
[1] => techbriefers-folder/sub-file-two.txt
[2] => techbriefers-folder/sub-folder-one
[3] => techbriefers-folder/sub-folder-two
)
*/
Here, we’re specifically looking inside the ‘techbriefers-folder’ directory. This helps when you want to narrow down your search to a specific area!
Conclusion
I hope this helps you understand how to use the glob()
function in PHP! It’s a powerful way to find files and directories based on your needs. Remember, practice makes perfect, so don’t hesitate to experiment with different patterns and flags. Happy coding!
The best I’ve seen so far. Thumbs up for a very good and easy to understand page.