A simple way to traverse directories in PHP
There are many functions in PHP that we rarely hear about, but they have very practical functions, such as: glob(). Many people want to simply traverse the directory. If you know this function, you will get twice the result with half the effort.
Glob has been included in the kernel since PHP4 and is not a new function, but like checkdnsrr(), few people know about this function. Let's take a look at how to use this function to traverse a directory.
Code
foreach(glob('dir/*.php') as $filename)
{
echo 'Filename: ' . $filename . '
' ;
}
Glob supports two parameters, the second of which is optional. The above code will return all files with the extension php in the dir directory.
Optional parameters
You can use the second parameter. achieve different purposes. For example, the following code returns two types of files in the dir directory
$aFiles = glob('{dir/*.jpg,myDirectory/*. gif}', GLOB_BRACE);
GLOB_BRACE tells glob() that I have used curly braces to expand two different file extensions.
PHP defines the following constants, which can be used as the second parameter
-
GLOB_MARK - price slash
in the middle of each returned result
-
GLOB_NOSORT - Return files unsorted (in the order they appear)
-
GLOB_NOCHECK - If the file is not found, return to search mode (such as the above {dir/*.jpg,myDirectory/*.gif})
-
GLOB_NOESCAPE - No escaping, that is, backslash is not used as an escaping character
-
GLOB_BRACE - Search pattern enclosed in braces
-
GLOB_ONLYDIR - Only return directory names that match the search criteria
-
GLOB_ERR - Stop automatically when encountering an error (default is to continue searching)
http://www.bkjia.com/PHPjc/629792.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629792.htmlTechArticleA simple way to traverse directories in PHP There are many functions in PHP that we rarely hear about, but there are Very practical functions, such as: glob(). Many people want to simply traverse the directory. If you know...