PHP glob() Function

PHP glob Function


Definition :

The PHP glob() function returns an array of filenames or directories matching a specified pattern. The function returns an array containing the matched files/directories or an empty array if no match is found. It returns false on error.

Syntax

  glob(patternflags)

Parameter 
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 returned
  • GLOB_NOSORT - Return files as they appear in the directory (unsorted)
  • GLOB_NOCHECK - Returns the search pattern if no match were found
  • GLOB_NOESCAPE - Backslashes do not quote metacharacters
  • GLOB_BRACE - Expands {a,b,c} to match 'a', 'b', or 'c'
  • GLOB_ONLYDIR - Return only directories which match the pattern
  • GLOB_ERR - (added in PHP 5.1) Stop on errors (errors are ignored by default)

Given below example glob() function is used to get the filenames of all text files in the current working directory :

Output : 

Array

(

    [0] => demo.txt

    [1] => file.txt

)