glob
Description
The glob of Filesystem for PHP find pathnames matching a pattern.
Syntax
glob(
string $pattern,
int $flags = 0
): array|falseParameters
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] => /home1
[5] => /home2
[6] => /lib
[7] => /lib64
[8] => /lscache
[9] => /opt
[10] => /proc
[11] => /root
[12] => /run
[13] => /sbin
[14] => /tmp
[15] => /usr
[16] => /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] => /home1/
[5] => /home2/
[6] => /lib/
[7] => /lib64/
[8] => /lscache/
[9] => /opt/
[10] => /proc/
[11] => /root/
[12] => /run/
[13] => /sbin/
[14] => /tmp/
[15] => /usr/
[16] => /var/
)
3 · flags · GLOB_NOSORT
<? $pattern = "/*"; $flags = GLOB_NOSORT; $return = glob($pattern, $flags); print_r($return);
Array
(
[0] => /lscache
[1] => /home1
[2] => /proc
[3] => /run
[4] => /opt
[5] => /lib64
[6] => /dev
[7] => /tmp
[8] => /home2
[9] => /root
[10] => /var
[11] => /home
[12] => /sbin
[13] => /usr
[14] => /etc
[15] => /bin
[16] => /lib
)
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/imunify360
[25] => /etc/inputrc
[26] => /etc/joe
[27] => /etc/ld.so.cache
[28] => /etc/ld.so.conf
[29] => /etc/ld.so.conf.d
[30] => /etc/localtime
[31] => /etc/mail
[32] => /etc/mail.rc
[33] => /etc/mailcap
[34] => /etc/mailhelo
[35] => /etc/mailips
[36] => /etc/mc
[37] => /etc/mime.types
[38] => /etc/modulefiles
[39] => /etc/motd.d
[40] => /etc/my.cnf
[41] => /etc/my.cnf.d
[42] => /etc/nsswitch.conf
[43] => /etc/openldap
[44] => /etc/pam.d
[45] => /etc/passwd
[46] => /etc/pki
[47] => /etc/profile
[48] => /etc/profile.d
[49] => /etc/protocols
[50] => /etc/recent_authed_mail_ips
[51] => /etc/relayhosts
[52] => /etc/resolv.conf
[53] => /etc/rpc
[54] => /etc/sasl2
[55] => /etc/scl
[56] => /etc/screenrc
[57] => /etc/security
[58] => /etc/senderverifybypasshosts
[59] => /etc/services
[60] => /etc/shadow
[61] => /etc/skel
[62] => /etc/skipsmtpcheckhosts
[63] => /etc/spammeripblocks
[64] => /etc/ssh
[65] => /etc/ssl
[66] => /etc/sysconfig
[67] => /etc/trusted-key.key
[68] => /etc/trustedmailhosts
[69] => /etc/vimrc
[70] => /etc/virc
[71] => /etc/wgetrc
[72] => /tmp/mysocket
[73] => /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/imunify360
[15] => /etc/joe
[16] => /etc/ld.so.conf.d
[17] => /etc/mail
[18] => /etc/mc
[19] => /etc/modulefiles
[20] => /etc/motd.d
[21] => /etc/my.cnf.d
[22] => /etc/openldap
[23] => /etc/pam.d
[24] => /etc/pki
[25] => /etc/profile.d
[26] => /etc/sasl2
[27] => /etc/scl
[28] => /etc/security
[29] => /etc/skel
[30] => /etc/ssh
[31] => /etc/ssl
[32] => /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