A simple way to traverse a directory in PHP
There are many functions in PHP, which we seldom hear, but have very useful functions, for example: Glob (). Many people want to simply traverse the directory, if you know the function, it will be more effective.
Glob is included in the kernel from PHP4, not a new function, but like CHECKDNSRR (), few people know about this function. Let's take a look at this. How to use this function to traverse a directory.
Code
foreach (Glob (' dir/*.php ') as $filename)
{
Echo ' Filename: '. $filename. '
';
}
The glob supports two parameters, and the second one is optional. The above code will return all files with the extension php under the dir directory.
Optional Parameters
You can use the second parameter. Achieve a different purpose. For example, the following code returns two types of files in the Dir directory
$aFiles = Glob (' {dir/*.jpg,mydirectory/*.gif} ', glob_brace);
Glob_braceTell Glob () that I used curly braces to amplify two different file extensions.
PHP defines the following constants, which can be used as a second parameter
- Glob_mark -Price slash in the middle of each returned result
- glob_nosort -return files are not sorted (in the order in which they appear)
- Glob_nocheck -Returns the search mode (e.g. {dir/*.jpg,mydirectory/*.gif} above) if the file is not found
- Glob_noescape -does not turn meaning, that is, the backslash is not used as a signifier character
- glob_brace -A search pattern enclosed in curly braces
- Glob_onlydir -returns only directory names that match the search criteria
- glob_err -Auto Stop encountered error (default is to continue looking for)
http://www.bkjia.com/PHPjc/629792.html www.bkjia.com true http://www.bkjia.com/PHPjc/629792.html techarticle a simple way to traverse a directory in PHP there are many functions in PHP, which we seldom hear, but have very useful functions, such as: Glob (). Many people want to simply traverse the directory, if you know ...