Menu Home

glob

osbo.com | PHP | Functions | glob

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

pattern

<?

$pattern = "/*";

$return = glob($pattern);

print_r($return);

?>
Array
(
    [0] => /bin
    [1] => /dev
    [2] => /etc
    [3] => /home
    [4] => /lib
    [5] => /lib64
    [6] => /opt
    [7] => /proc
    [8] => /root
    [9] => /sbin
    [10] => /scripts
    [11] => /tmp
    [12] => /usr
    [13] => /var
)

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] => /opt/
    [7] => /proc/
    [8] => /root/
    [9] => /sbin/
    [10] => /scripts/
    [11] => /tmp/
    [12] => /usr/
    [13] => /var/
)

flags | GLOB_NOSORT

<?

$pattern = "/*";
$flags = GLOB_NOSORT;

$return = glob($pattern, $flags);

print_r($return);

?>
Array
(
    [0] => /root
    [1] => /bin
    [2] => /home
    [3] => /proc
    [4] => /opt
    [5] => /lib64
    [6] => /sbin
    [7] => /var
    [8] => /usr
    [9] => /dev
    [10] => /scripts
    [11] => /etc
    [12] => /tmp
    [13] => /lib
)

flags | GLOB_NOCHECK

<?

$pattern = "pattern";
$flags = GLOB_NOCHECK;

$return = glob($pattern, $flags);

print_r($return);

?>
Array
(
    [0] => pattern
)

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
    [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/clamd.conf
    [17] => /etc/cpanel
    [18] => /etc/cron.daily
    [19] => /etc/default
    [20] => /etc/domainusers
    [21] => /etc/environment
    [22] => /etc/fonts
    [23] => /etc/gcrypt
    [24] => /etc/ghostscript
    [25] => /etc/group
    [26] => /etc/host.conf
    [27] => /etc/hosts
    [28] => /etc/inputrc
    [29] => /etc/ld.so.cache
    [30] => /etc/ld.so.conf
    [31] => /etc/ld.so.conf.d
    [32] => /etc/lftp.conf
    [33] => /etc/localtime
    [34] => /etc/lynx-site.cfg
    [35] => /etc/lynx.cfg
    [36] => /etc/lynx.lss
    [37] => /etc/mail
    [38] => /etc/mail.rc
    [39] => /etc/mailcap
    [40] => /etc/mailhelo
    [41] => /etc/mailips
    [42] => /etc/man.config
    [43] => /etc/mime.types
    [44] => /etc/my.cnf
    [45] => /etc/my.cnf.d
    [46] => /etc/nsswitch.conf
    [47] => /etc/odbc.ini
    [48] => /etc/odbcinst.ini
    [49] => /etc/openldap
    [50] => /etc/pam.d
    [51] => /etc/pango
    [52] => /etc/passwd
    [53] => /etc/pki
    [54] => /etc/profile
    [55] => /etc/profile.d
    [56] => /etc/protocols
    [57] => /etc/rc.d
    [58] => /etc/resolv.conf
    [59] => /etc/rpc
    [60] => /etc/sasl2
    [61] => /etc/scl
    [62] => /etc/screenrc
    [63] => /etc/security
    [64] => /etc/senderverifybypasshosts
    [65] => /etc/services
    [66] => /etc/shadow
    [67] => /etc/skel
    [68] => /etc/skipsmtpcheckhosts
    [69] => /etc/snmp
    [70] => /etc/spammeripblocks
    [71] => /etc/ssh
    [72] => /etc/ssl
    [73] => /etc/sysconfig
    [74] => /etc/trustedmailhosts
    [75] => /etc/udev
    [76] => /etc/vimrc
    [77] => /etc/virc
    [78] => /etc/wgetrc
    [79] => /etc/xinetd.d
    [80] => /etc/zlogin
    [81] => /etc/zlogout
    [82] => /etc/zprofile
    [83] => /etc/zshenv
    [84] => /etc/zshrc
    [85] => /tmp/mysql.sock
)

flags | GLOB_ONLYDIR

<?

$pattern = "/etc/*";
$flags = GLOB_ONLYDIR;

$return = glob($pattern, $flags);

print_r($return);

?>
Array
(
    [0] => /etc/ImageMagick
    [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/cron.daily
    [10] => /etc/default
    [11] => /etc/fonts
    [12] => /etc/gcrypt
    [13] => /etc/ghostscript
    [14] => /etc/ld.so.conf.d
    [15] => /etc/mail
    [16] => /etc/my.cnf.d
    [17] => /etc/openldap
    [18] => /etc/pam.d
    [19] => /etc/pango
    [20] => /etc/pki
    [21] => /etc/profile.d
    [22] => /etc/rc.d
    [23] => /etc/sasl2
    [24] => /etc/scl
    [25] => /etc/security
    [26] => /etc/skel
    [27] => /etc/snmp
    [28] => /etc/ssh
    [29] => /etc/ssl
    [30] => /etc/sysconfig
    [31] => /etc/udev
    [32] => /etc/xinetd.d
)

flags | GLOB_ERR

<?

$pattern = "/root/*";
$flags = GLOB_ERR;

$return = glob($pattern, $flags);

print_r($return);

?>

			
			
		

PHP