Use the Glob method to traverse all files under a folder

Source: Internet
Author: User
Tags glob
Traversing all files under a folder, you can generally use the Opendir and Readdir methods to traverse.

Example: Find all the PHP files in the specified directory (do not search subfolders), the code is as follows:


    
     $path = dirname(__FILE__);$result = traversing($path);print_r($result);functiontraversing($path){$result = array();    if($handle = opendir($path)){        while($file=readdir($handle)){            if($file!='.' && $file!='..'){                if(strtolower(substr($file, -4))=='.php'){                    array_push($result, $file);                }            }        }    }    return$result;}?>

If you use the Glob method to traverse, you can simplify the code


    
     $path = dirname(__FILE__);$result = glob($path.'/*.php');print_r($result);?>

Note that Glob returns the path to the path+ search result, such as path= '/home/fdipzone ', and the above example returns

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

This is a different place than the result returned by Opendir,readdir.

If you just traverse the current directory. Can be changed to this: Glob (' *.php ');
Glob Syntax Description:

glob$patternint$flags0 ] )

The Glob () function looks for all file paths that match the pattern in accordance with the rules used by the libc glob () function, similar to the rules used for general shells. No abbreviation extensions or parameter overrides are performed. Glob is powerful with regular matching paths.

Flags valid tokens are:
Glob_mark-Add a slash to each returned item
Glob_nosort-Returns (not sorted) according to the original order in which the files appear in the directory
Glob_nocheck-Returns the mode used for search if no file matches
Glob_noescape-Backslash does not escape meta-characters
Glob_brace-expand {a,b,c} to match ' A ', ' B ' or ' C '
Glob_onlydir-Returns only catalog entries that match the pattern
Glob_err-Stop and read error messages (such as unreadable directories), ignoring all errors by default

Example: Use the Glob method to traverse all PHP files under a specified folder (including subfolders)

   !--? php   $path  = dirname ( __ file__ );  $result  =  array  (), traversing ( $path ,  $result );p Rint_r ( $ Result );   function   traversing   ( $path , &  $result )  {   $curr  = glob ( $path .  '    /*');  if  ( $curr ) { foreach  ( $curr   as   $f ) { if  (Is_dir ( $                F )) {Array_push ( $result ,  $f );            Traversing ( $f ,  $result ); }  ElseIf  (Strtolower (substr ( $f ,- 4 )) = =  '. php '  ' {Array_push            ( $result ,  $f ); }}}} ?     

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The above describes the use of the Glob method to traverse the folder under all the files, including the content, I hope the PHP tutorial interested in a friend helpful.

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