Name

glob

Synopsis

globarray(string pattern[, int flags])

Returns a list of filenames matching the shell wildcard pattern given in pattern. The following characters and sequences make matches:

*

Matches any number of any character (equivalent to the regex pattern .*)

?

Matches any one character (equivalent to the regex pattern .)

For example, to process every JPEG file in a particular directory, you might write:

foreach(glob("/tmp/images/*.jpg") as $filename) {
    // do something with $filename
}

The flags value is a bitwise OR of any of the following values:

GLOB_MARK

Adds a slash to each item returned.

GLOB_NOSORT

Returns files in the same order as found in the directory itself. If this is not specified, the names are sorted by ASCII value.

GLOB_NOCHECK

If no files matching pattern are found, pattern is returned.

GLOB_NOESCAPE

Treat backslashes in pattern as backslashes, rather than as the start of an escape sequence.

GLOB_BRACE

In addition to the normal matches, strings in the form {foo, bar, baz} match either “foo”, “bar”, or “baz”.

GLOB_ONLYDIR

Returns only directories matching pattern.

GLOB_ERR

Stop on read errors.

Get Programming PHP, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.