Find pathnames matching a pattern
Syntax
glob ( string $pattern [, int $flags = 0 ] ) : array
Parameters
pattern
The pattern. No tilde expansion or parameter substitution is done. Special characters:
* | Matches zero or more characters. |
? | Matches exactly one character (any character). |
[...] | Matches one character from a group of characters. If the first character is !, matches any character not in the group. |
\ | Escapes the following character, except when the GLOB_NOESCAPE flag is used. |
flags
GLOB_MARK | Adds a slash to each directory returned |
GLOB_NOSORT | Return files as they appear in the directory (no sorting). When this flag is not used, the pathnames are sorted alphabetically |
GLOB_NOCHECK | Return the search pattern if no files matching it 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 directory entries which match the pattern |
GLOB_ERR | Stop on read errors (like unreadable directories), by default errors are ignored. |
Return
Returns an array containing the matched files/directories, an empty array if no file matched or FALSE on error.
Note: On some systems it is impossible to distinguish between empty match and an error.
Examples
1 · pattern
<? $pattern = "/*"; $return = glob($pattern); print_r($return); ?>
Array ( [0] => /bin [1] => /dev [2] => /etc [3] => /home [4] => /home3 [5] => /lib [6] => /lib64 [7] => /lscache [8] => /opt [9] => /proc [10] => /root [11] => /run [12] => /sbin [13] => /tmp [14] => /usr [15] => /var )
2 · flags · GLOB_MARK
<? $pattern = "/*"; $flags = GLOB_MARK; $return = glob($pattern, $flags); print_r($return); ?>
Array ( [0] => /bin/ [1] => /dev/ [2] => /etc/ [3] => /home/ [4] => /home3/ [5] => /lib/ [6] => /lib64/ [7] => /lscache/ [8] => /opt/ [9] => /proc/ [10] => /root/ [11] => /run/ [12] => /sbin/ [13] => /tmp/ [14] => /usr/ [15] => /var/ )
3 · flags · GLOB_NOSORT
<? $pattern = "/*"; $flags = GLOB_NOSORT; $return = glob($pattern, $flags); print_r($return); ?>
Array ( [0] => /opt [1] => /home [2] => /root [3] => /run [4] => /usr [5] => /bin [6] => /home3 [7] => /etc [8] => /lib64 [9] => /lib [10] => /lscache [11] => /sbin [12] => /proc [13] => /tmp [14] => /var [15] => /dev )
4 · flags · GLOB_NOCHECK
<? $pattern = "pattern"; $flags = GLOB_NOCHECK; $return = glob($pattern, $flags); print_r($return); ?>
Array ( [0] => pattern )
5 · flags · GLOB_BRACE
<? $pattern = "/{etc,tmp}/*"; $flags = GLOB_BRACE; $return = glob($pattern, $flags); print_r($return); ?>
Array ( [0] => /etc/DIR_COLORS [1] => /etc/DIR_COLORS.256color [2] => /etc/DIR_COLORS.lightbgcolor [3] => /etc/ImageMagick-6 [4] => /etc/aliases [5] => /etc/aliases.db [6] => /etc/alternatives [7] => /etc/apache2 [8] => /etc/at.deny [9] => /etc/backupmxhosts [10] => /etc/bash_completion.d [11] => /etc/bashrc [12] => /etc/cl.nodejs [13] => /etc/cl.php.d [14] => /etc/cl.python [15] => /etc/cl.selector [16] => /etc/cpanel [17] => /etc/default [18] => /etc/domainusers [19] => /etc/environment [20] => /etc/fonts [21] => /etc/gcrypt [22] => /etc/ghostscript [23] => /etc/group [24] => /etc/host.conf [25] => /etc/hosts [26] => /etc/imunify360 [27] => /etc/inputrc [28] => /etc/joe [29] => /etc/ld.so.cache [30] => /etc/ld.so.conf [31] => /etc/ld.so.conf.d [32] => /etc/localtime [33] => /etc/mail [34] => /etc/mail.rc [35] => /etc/mailcap [36] => /etc/mailhelo [37] => /etc/mailips [38] => /etc/mc [39] => /etc/mime.types [40] => /etc/my.cnf [41] => /etc/my.cnf.d [42] => /etc/nsswitch.conf [43] => /etc/odbcinst.ini [44] => /etc/openldap [45] => /etc/pam.d [46] => /etc/passwd [47] => /etc/pki [48] => /etc/profile [49] => /etc/profile.d [50] => /etc/protocols [51] => /etc/rarfiles.lst [52] => /etc/resolv.conf [53] => /etc/rpc [54] => /etc/rpm [55] => /etc/rsyncd.conf [56] => /etc/sasl2 [57] => /etc/scl [58] => /etc/screenrc [59] => /etc/security [60] => /etc/senderverifybypasshosts [61] => /etc/services [62] => /etc/shadow [63] => /etc/skel [64] => /etc/skipsmtpcheckhosts [65] => /etc/spammeripblocks [66] => /etc/ssh [67] => /etc/ssl [68] => /etc/sysconfig [69] => /etc/trusted-key.key [70] => /etc/trustedmailhosts [71] => /etc/vimrc [72] => /etc/virc [73] => /etc/wgetrc [74] => /tmp/mysql.sock )
6 · flags · GLOB_ONLYDIR
<? $pattern = "/etc/*"; $flags = GLOB_ONLYDIR; $return = glob($pattern, $flags); print_r($return); ?>
Array ( [0] => /etc/ImageMagick-6 [1] => /etc/alternatives [2] => /etc/apache2 [3] => /etc/bash_completion.d [4] => /etc/cl.nodejs [5] => /etc/cl.php.d [6] => /etc/cl.python [7] => /etc/cl.selector [8] => /etc/cpanel [9] => /etc/default [10] => /etc/fonts [11] => /etc/gcrypt [12] => /etc/ghostscript [13] => /etc/imunify360 [14] => /etc/joe [15] => /etc/ld.so.conf.d [16] => /etc/mail [17] => /etc/mc [18] => /etc/my.cnf.d [19] => /etc/openldap [20] => /etc/pam.d [21] => /etc/pki [22] => /etc/profile.d [23] => /etc/rpm [24] => /etc/sasl2 [25] => /etc/scl [26] => /etc/security [27] => /etc/skel [28] => /etc/ssh [29] => /etc/ssl [30] => /etc/sysconfig )
7 · flags · GLOB_ERR
<? $pattern = "/root/*"; $flags = GLOB_ERR; $return = glob($pattern, $flags); print_r($return); ?>