In this article, you will learn how to get the list of files or folders whose name matches with a certain pattern in PHP. The glob() function in PHP returns an array of filenames or directories matching a specified pattern.
what is the syntax of the GLOB function in php?
glob(pattern, flags)
Parameter | Description |
---|---|
pattern | Required. Specifies the pattern to search for |
flags | Optional. Specifies special settings.Possible values:GLOB_MARK – Adds a slash to each item returnedGLOB_NOSORT – Return files as they appear in the directory (unsorted)GLOB_NOCHECK – Returns the search pattern if no match were foundGLOB_NOESCAPE – Backslashes do not quote metacharactersGLOB_BRACE – Expands {a,b,c} to match ‘a’, ‘b’, or ‘c’GLOB_ONLYDIR – Return only directories which match the patternGLOB_ERR – (added in PHP 5.1) Stop on errors (errors are ignored by default) |
examples of the GLOB function
Example 1. In this example, we return an array of filenames or directories that matches the specified pattern.
<?php
print_r(glob("*.txt"));
?>