Use glob to traverse all files in the folder

Source: Internet
Author: User
: This article describes how to use glob to traverse all files in a folder. For more information about PHP tutorials, see. You can use opendir and readdir to traverse all files in a folder.

Example: Find all php files in the specified directory (do not search for subfolders). The code is as follows:


  

For example, you can use the glob method to traverse the code.


  

Note that glob returns path + search result path, for example, path = '/home/fdipzone '.

Array(    [0] => /home/fdipzone/a.php    [1] => /home/fdipzone/b.php    [2] => /home/fdipzone/c.php)

This is different from the result returned by opendir and readdir.

If you only want to traverse the current directory. You can change it to glob ('*. php ');
Glob syntax description:

array glob ( string $pattern [, int$flags = 0 ] )

The glob () function searches for all file paths that match the pattern according to the rules used by the libc glob () function, similar to the rules used by General shells. Do not perform abbreviation extension or parameter substitution. Glob uses the regular expression matching path.

Valid flags include:
GLOB_MARK-add a diagonal line to each returned item
GLOB_NOSORT-returns (unordered) files in the original order in the directory)
GLOB_NOCHECK-returns the search mode if no matching file exists.
GLOB_NOESCAPE
GLOB_BRACE-extended {a, B, c} to match 'A', 'B' or 'C'
GLOB_ONLYDIR-returns only Directory items that match the pattern.
GLOB_ERR-stop and read error messages (such as unreadable directories). all errors are ignored by default.

Example: use glob to traverse all php files in a specified folder (including subfolders)


  

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

The above describes how to use the glob method to traverse all the files in the folder, including the content, and hope to help friends who are interested in PHP tutorials.

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.