Using PHP function Glob to find the file path matching the pattern, mainly discusses the function and usage of glob () functions, using the GLOB function to read the directory faster than the others, because the Glob function is the built-in function is naturally faster to process.
One, function prototypes
array Glob (string pattern [, int flags])
Note: the Glob () function gets an array that returns an array containing the matching files/directories. False if error is returned
Two, version compatible
PHP 4 >= 4.3.0, PHP 5
Third, the basic usage and examples of functions
1. Files that match the. txt suffix in the directory
<?php foreach (Glob ("*.txt") as $filename) { echo $filename;}?>
2, for compatible case matching
<?php $pattern = sql_case ("*.pdf"); Var_dump (Glob ($pattern));?>
Similar to the following
<?php foreach (Array_merge (Glob ("*.pdf"), Glob ("*. PDF ") as $filename) { echo" $filename n ";}?>
3, get all subdirectories under directory
<?php function Listdirs ($dir) {Static $alldirs = array (); $dirs = Glob ($dir. '/* ', glob_onlydir); if (count ($dirs) > 0) {foreach ($dirs as $d) $alldirs [] = $d;} foreach ($dirs as $dir) listdirs ($dir); return $alldirs; }?>
4. Match All Files
<?php $files = Glob (' {,.} * ', glob_brace);?>
Four, precautions
1, cannot act on remote files, the file being inspected must be accessed through the server's file system.
2, using Glob ("[myfolder]/*.txt") will not match, the workaround is Glob ("[Myfolder]/*.txt"), note [] character is applied.
3, followed by the second parameter flags valid tag description
(1) Glob_mark-Add a slash to each returned item
(2) Glob_nosort-Returns (not sorted) according to the original order in which the files appear in the directory
(3) Glob_nocheck-Returns the mode for search if no file matches
(4) Glob_noescape-Backslash does not escape the metacharacters
(5) Glob_brace-expand {a,b,c} to match ' A ', ' B ' or ' C '
(6) Glob_onlydir-returns only directory entries that match the pattern Note: before PHP 4.3.3, Glob_onlydir is not available on Windows or other systems that do not use the GNU C library.
(7) Glob_err-stop and read error messages (such as unreadable directories), ignoring all errors by default note: Glob_err is added by PHP 5.1.
The typical application of the glob () function is to read a data table file, such as obtaining a. sql suffix file in a directory, which is very useful in unit testing and can be used to read SQL file rebuilding database, etc., please participate in PHP manual.
Other references:
Example 1
<?phpprint_r (Glob ("*.txt"));? >
The output is similar to:
Array ([0] = target.txt[1] = source.txt[2] = test.txt[3] = test2.txt)
Example 2
<?phpprint_r (Glob ("* *"));? >
The output is similar to:
Array ([0] = contacts.csv[1] (= default.php[2] = target.txt[3] = source.txt[4] = tem1.tmp[5] = Test. HTM[6] = test.ini[7] [test.php[8] = test.txt[9] = test2.txt)
PHP glob () function to implement directory file traversal and find the file path matching the pattern