Php glob implements directory file traversal and searches for file paths matching the pattern

Source: Internet
Author: User

I. Function prototype
Array glob (string pattern [, int flags])
Note: The glob () function returns an array containing matching files/directories. If an error occurs, FALSE is returned.

2. Version compatibility
PHP 4> = 4.3.0, PHP 5

III. Basic functions and examples

1. Directory file with the suffix .txt


 

The code is as follows: Copy code
<? Php
Foreach (glob ("*. txt") as $ filename ){
Echo $ filename;
}
?>


2. Enable case-sensitive matching.


 

The code is as follows: Copy code
<? Php
$ Pattern = SQL _case ("*. pdf ");
Var_dump (glob ($ pattern ));
?>


Similar


 

The code is as follows: Copy code
<? Php
Foreach (array_merge (glob ("*. pdf"), glob ("*. PDF") as $ filename ){
Echo "$ filename n ";
}
?>


3. Obtain all subdirectories in the directory.


 

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

 

The code is as follows: Copy code
<? Php
$ Files = glob ('{,.} *', GLOB_BRACE );
?>


IV. Notes
1. It cannot act on remote files. The file to be checked must be accessed through the file system of the server.
2. Use glob ("[myfolder]/*. txt ") will not match, solution is glob (" [myfolder]/*. txt "), pay attention to the [] character application.
3, followed by the description of the second parameter flags
(1) GLOB_MARK-add a diagonal line to each returned project
(2) GLOB_NOSORT-returned in the original order of the file in the Directory (unordered)
(3) GLOB_NOCHECK-if there is no file match, return the search mode
(4) GLOB_NOESCAPE-the backslash does not convert the metacharacters
(5) GLOB_BRACE-extended {a, B, c} to match 'A', 'B' or 'C'
(6) GLOB_ONLYDIR-returns only Directory items that match the pattern. Note: GLOB_ONLYDIR is unavailable on Windows or other systems that do not use the gnu c Library before PHP 4.3.3.
(7) GLOB_ERR-stop and read error messages (such as unreadable directories). By default, all errors are ignored. Note: GLOB_ERR is added in PHP 5.1.

A typical application of the glob () function is to read data table files, for example, to obtain files in a directory. SQL suffix files, which are very practical in unit tests and can be used to read SQL files and recreate databases. For details, please refer to the PHP Manual. Please pay attention to the next PHP built-in function Research Series.

Other references

 

The code is as follows: Copy code

Example 1
<? Php
Print_r (glob ("*. txt "));
?> The output is similar:

Array
(
[0] => target.txt
[1] => source.txt
[2] => test.txt
[3] => test2.txt
) Example 2
<? Php
Print_r (glob ("*.*"));
?> The output is similar:

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
)

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.