Home > Backend Development > PHP Tutorial > A simple way to traverse directories in PHP_PHP Tutorial

A simple way to traverse directories in PHP_PHP Tutorial

WBOY
Release: 2016-07-13 17:09:04
Original
856 people have browsed it

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)

www.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...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template