PHP glob Implement directory file traversal and find the file path matching the pattern _php tutorial

Source: Internet
Author: User
Tags glob
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






2 for compatible case matching


foreach (glob (" *.txt ") as $file Name) { echo $filename; }?>
code as follows copy code
code as follows copy code
$pattern = sql_case ("*.pdf");
Var_dump (Glob ($pattern));
?


Similar to the following


The code is as follows Copy Code
foreach (Array_merge (Glob ("*.pdf"), Glob ("*. PDF ")) as $filename) {
echo "$filename n";
}
?>


3, get all subdirectories under directory


The code is as follows Copy Code
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

TD bgcolor= "#FFE7CE" height= "width=" 464 "> code as follows
copy code
$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.

A typical application of the glob () function is to read a data table file, such as getting a. sql suffix file in a directory, which is very useful in unit testing, can implement read SQL file rebuilding database, etc., please participate in PHP Manual, please follow the next issue of PHP built-in function research series

Other references

The code is as follows Copy Code

Example 1
Print_r (Glob ("*.txt"));
?> output is similar to:

Array
(
[0] = Target.txt
[1] = Source.txt
[2] = Test.txt
[3] = Test2.txt
) Example 2
Print_r (Glob ("* *"));
?> 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
)


http://www.bkjia.com/PHPjc/445313.html www.bkjia.com true http://www.bkjia.com/PHPjc/445313.html techarticle using PHP function Glob to find the file path matching the pattern, mainly discusses the function and usage of glob () functions, using Glob function to read the directory faster than the others, because the Glob function is built ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.