PHP Tutorial - PHP glob() Function






Definition

The glob() function returns an array of filenames or directories matching a specified pattern.

Syntax

PHP glob() Function has the following syntax.

glob(pattern,flags)

Parameter

ParameterIs RequiredDescription
patternRequired.Pattern to search for
flagsOptional.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 - Stop on errors. Errors are ignored by default




Return

This function returns an array of files/directories, or FALSE on failure.

Example

Returns an array of filenames or directories matching a specified pattern


<?php
print_r(glob("*.txt"));
print_r(glob("*.*"));
?>

The code above generates the following result.