Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

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:

PatternDescription
*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

ConstantDescription
GLOB_MARKAdds a slash to each directory returned
GLOB_NOSORTReturn files as they appear in the directory (no sorting). When this flag is not used, the pathnames are sorted alphabetically
GLOB_NOCHECKReturn the search pattern if no files matching it were found
GLOB_NOESCAPEBackslashes do not quote metacharacters
GLOB_BRACEExpands {a,b,c} to match 'a', 'b', or 'c'
GLOB_ONLYDIRReturn only directory entries which match the pattern
GLOB_ERRStop 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] => /home2
    [5] => /home3
    [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] => /home2/
    [5] => /home3/
    [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] => /opt
    [1] => /home
    [2] => /root
    [3] => /home2
    [4] => /run
    [5] => /usr
    [6] => /bin
    [7] => /home3
    [8] => /etc
    [9] => /lib64
    [10] => /lib
    [11] => /lscache
    [12] => /sbin
    [13] => /proc
    [14] => /tmp
    [15] => /var
    [16] => /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/cl.selector.conf.d
    [17] => /etc/cpanel
    [18] => /etc/default
    [19] => /etc/domainusers
    [20] => /etc/environment
    [21] => /etc/fonts
    [22] => /etc/gcrypt
    [23] => /etc/ghostscript
    [24] => /etc/group
    [25] => /etc/host.conf
    [26] => /etc/hosts
    [27] => /etc/imunify360
    [28] => /etc/inputrc
    [29] => /etc/joe
    [30] => /etc/ld.so.cache
    [31] => /etc/ld.so.conf
    [32] => /etc/ld.so.conf.d
    [33] => /etc/localtime
    [34] => /etc/mail
    [35] => /etc/mail.rc
    [36] => /etc/mailcap
    [37] => /etc/mailhelo
    [38] => /etc/mailips
    [39] => /etc/mc
    [40] => /etc/mime.types
    [41] => /etc/my.cnf
    [42] => /etc/my.cnf.d
    [43] => /etc/nsswitch.conf
    [44] => /etc/odbcinst.ini
    [45] => /etc/openldap
    [46] => /etc/pam.d
    [47] => /etc/passwd
    [48] => /etc/pki
    [49] => /etc/profile
    [50] => /etc/profile.d
    [51] => /etc/protocols
    [52] => /etc/rarfiles.lst
    [53] => /etc/recent_authed_mail_ips
    [54] => /etc/relayhosts
    [55] => /etc/resolv.conf
    [56] => /etc/rpc
    [57] => /etc/rpm
    [58] => /etc/rsyncd.conf
    [59] => /etc/sasl2
    [60] => /etc/scl
    [61] => /etc/screenrc
    [62] => /etc/security
    [63] => /etc/senderverifybypasshosts
    [64] => /etc/services
    [65] => /etc/shadow
    [66] => /etc/skel
    [67] => /etc/skipsmtpcheckhosts
    [68] => /etc/spammeripblocks
    [69] => /etc/ssh
    [70] => /etc/ssl
    [71] => /etc/sysconfig
    [72] => /etc/trusted-key.key
    [73] => /etc/trustedmailhosts
    [74] => /etc/vimrc
    [75] => /etc/virc
    [76] => /etc/wgetrc
    [77] => /tmp/mysocket
    [78] => /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/cl.selector.conf.d
    [9] => /etc/cpanel
    [10] => /etc/default
    [11] => /etc/fonts
    [12] => /etc/gcrypt
    [13] => /etc/ghostscript
    [14] => /etc/imunify360
    [15] => /etc/joe
    [16] => /etc/ld.so.conf.d
    [17] => /etc/mail
    [18] => /etc/mc
    [19] => /etc/my.cnf.d
    [20] => /etc/openldap
    [21] => /etc/pam.d
    [22] => /etc/pki
    [23] => /etc/profile.d
    [24] => /etc/rpm
    [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
HomeMenu