glob
Description
The glob of Filesystem for PHP find pathnames matching a pattern.
Syntax
glob( string $pattern, int $flags = 0 ): array|false
Parameters
pattern
The pattern. No tilde expansion or parameter substitution is done. Special characters:
Pattern | Description |
---|---|
* | 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
Constant | Description |
---|---|
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] => /lib [5] => /lib64 [6] => /lscache [7] => /opt [8] => /proc [9] => /root [10] => /run [11] => /sbin [12] => /tmp [13] => /usr [14] => /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] => /lib/ [5] => /lib64/ [6] => /lscache/ [7] => /opt/ [8] => /proc/ [9] => /root/ [10] => /run/ [11] => /sbin/ [12] => /tmp/ [13] => /usr/ [14] => /var/ )
3 · flags · GLOB_NOSORT
<? $pattern = "/*"; $flags = GLOB_NOSORT; $return = glob($pattern, $flags); print_r($return);
Array ( [0] => /home [1] => /dev [2] => /run [3] => /var [4] => /lib [5] => /tmp [6] => /lib64 [7] => /lscache [8] => /opt [9] => /root [10] => /proc [11] => /sbin [12] => /usr [13] => /etc [14] => /bin )
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/aliases [2] => /etc/alternatives [3] => /etc/apache2 [4] => /etc/at.deny [5] => /etc/backupmxhosts [6] => /etc/bash_completion.d [7] => /etc/bashrc [8] => /etc/cl.nodejs [9] => /etc/cl.php.d [10] => /etc/cl.python [11] => /etc/cl.selector [12] => /etc/cl.selector.conf.d [13] => /etc/cpanel [14] => /etc/crypto-policies [15] => /etc/default [16] => /etc/domainusers [17] => /etc/environment [18] => /etc/environment-modules [19] => /etc/fonts [20] => /etc/gcrypt [21] => /etc/group [22] => /etc/host.conf [23] => /etc/hosts [24] => /etc/inputrc [25] => /etc/joe [26] => /etc/ld.so.cache [27] => /etc/ld.so.conf [28] => /etc/ld.so.conf.d [29] => /etc/localtime [30] => /etc/mail [31] => /etc/mail.rc [32] => /etc/mailcap [33] => /etc/mailhelo [34] => /etc/mailips [35] => /etc/mc [36] => /etc/mime.types [37] => /etc/modulefiles [38] => /etc/motd.d [39] => /etc/my.cnf [40] => /etc/my.cnf.d [41] => /etc/nsswitch.conf [42] => /etc/openldap [43] => /etc/pam.d [44] => /etc/passwd [45] => /etc/pki [46] => /etc/profile [47] => /etc/profile.d [48] => /etc/protocols [49] => /etc/recent_authed_mail_ips [50] => /etc/relayhosts [51] => /etc/resolv.conf [52] => /etc/rpc [53] => /etc/sasl2 [54] => /etc/scl [55] => /etc/screenrc [56] => /etc/security [57] => /etc/senderverifybypasshosts [58] => /etc/services [59] => /etc/shadow [60] => /etc/skel [61] => /etc/skipsmtpcheckhosts [62] => /etc/spammeripblocks [63] => /etc/ssh [64] => /etc/ssl [65] => /etc/sysconfig [66] => /etc/trusted-key.key [67] => /etc/trustedmailhosts [68] => /etc/vimrc [69] => /etc/virc [70] => /etc/wgetrc [71] => /tmp/mysocket [72] => /tmp/mysql.sock )
6 · flags · GLOB_ONLYDIR
<? $pattern = "/etc/*"; $flags = GLOB_ONLYDIR; $return = glob($pattern, $flags); print_r($return);
Array ( [0] => /etc/alternatives [1] => /etc/apache2 [2] => /etc/bash_completion.d [3] => /etc/cl.nodejs [4] => /etc/cl.php.d [5] => /etc/cl.python [6] => /etc/cl.selector [7] => /etc/cl.selector.conf.d [8] => /etc/cpanel [9] => /etc/crypto-policies [10] => /etc/default [11] => /etc/environment-modules [12] => /etc/fonts [13] => /etc/gcrypt [14] => /etc/joe [15] => /etc/ld.so.conf.d [16] => /etc/mail [17] => /etc/mc [18] => /etc/modulefiles [19] => /etc/motd.d [20] => /etc/my.cnf.d [21] => /etc/openldap [22] => /etc/pam.d [23] => /etc/pki [24] => /etc/profile.d [25] => /etc/sasl2 [26] => /etc/scl [27] => /etc/security [28] => /etc/skel [29] => /etc/ssh [30] => /etc/ssl [31] => /etc/sysconfig )
7 · flags · GLOB_ERR
<? $pattern = "/root/*"; $flags = GLOB_ERR; $return = glob($pattern, $flags); var_export($return);
false
Links
Filesystem
- basename
- chgrp
- chmod
- chown
- clearstatcache
- copy
- dirname
- disk_free_space
- disk_total_space
- diskfreespace
- fclose
- feof
- fflush
- fgetc
- fgetcsv
- fgets
- file
- file_exists
- file_get_contents
- file_put_contents
- fileatime
- filectime
- filegroup
- fileinode
- filemtime
- fileowner
- fileperms
- filesize
- filetype
- flock
- fnmatch
- fopen
- fpassthru
- fputcsv
- fputs
- fread
- fscanf
- fseek
- fstat
- ftell
- ftruncate
- fwrite
- is_dir
- is_executable
- is_file
- is_link
- is_readable
- is_uploaded_file
- is_writable
- is_writeable
- lchgrp
- lchown
- link
- linkinfo
- lstat
- mkdir
- move_uploaded_file
- pathinfo
- pclose
- popen
- readfile
- readlink
- realpath
- realpath_cache_get
- realpath_cache_size
- rename
- rewind
- rmdir
- set_file_buffer
- stat
- symlink
- tempnam
- tmpfile
- touch
- umask
- unlink