A simple way to traverse directories in PHP _php tutorial

Source: Internet
Author: User
Tags glob

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 ...

  • 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.