Home  >  Article  >  Backend Development  >  Use the glob method to traverse all files in a folder

Use the glob method to traverse all files in a folder

WBOY
WBOYOriginal
2016-07-30 13:30:481002browse

Traverse all files in a folder. Generally, you can use the opendir and readdir methods to traverse.

Example: Find all php files in the specified directory (without searching subfolders), the code is as follows:

<code><span><span><?php</span><span>$path</span> = dirname(<span>__FILE__</span>);
<span>$result</span> = traversing(<span>$path</span>);
print_r(<span>$result</span>);

<span><span>function</span><span>traversing</span><span>(<span>$path</span>)</span>{</span><span>$result</span> = <span>array</span>();
    <span>if</span>(<span>$handle</span> = opendir(<span>$path</span>)){
        <span>while</span>(<span>$file</span>=readdir(<span>$handle</span>)){
            <span>if</span>(<span>$file</span>!=<span>'.'</span> && <span>$file</span>!=<span>'..'</span>){
                <span>if</span>(strtolower(substr(<span>$file</span>, -<span>4</span>))==<span>'.php'</span>){
                    array_push(<span>$result</span>, <span>$file</span>);
                }
            }
        }
    }
    <span>return</span><span>$result</span>;
}
<span>?></span></span></code>

If you use the glob method to traverse, the code can be simplified

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

Note that glob returns It will be the path of path+search results, for example path='/home/fdipzone'. The above example returns

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

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

If you just traverse the current directory. It can be changed to this: glob('*.php');
glob syntax description:

<code>array <span>glob</span> ( string <span>$pattern</span> [, <span>int</span><span>$flags</span> = <span>0</span> ] )</code>

glob() The function follows the rules used by the libc glob() function to find all matches with pattern The file path is similar to the rules used by general shells. No abbreviation expansion or parameter substitution is performed. Glob is powerful in using regular path matching.

flags Valid flags are:
GLOB_MARK - add a slash to each returned item
GLOB_NOSORT - return the files in the original order they appear in the directory (not sorted)
GLOB_NOCHECK - if there are no files Match returns the pattern used for searching
GLOB_NOESCAPE - backslash unescaped metacharacter
GLOB_BRACE - expands {a,b,c} to match 'a', 'b' or 'c'
GLOB_ONLYDIR - Return only directory entries matching the pattern
GLOB_ERR - Stop and read error information (such as unreadable directories), ignore all errors by default

Example: use the glob method to traverse the specified folder (including subfolder)

<code><span><span><?php</span><span>$path</span> = dirname(<span>__FILE__</span>);
<span>$result</span> = <span>array</span>();
traversing(<span>$path</span>, <span>$result</span>);
print_r(<span>$result</span>);

<span><span>function</span><span>traversing</span><span>(<span>$path</span>, &<span>$result</span>)</span>{</span><span>$curr</span> = glob(<span>$path</span>.<span>'/*'</span>);
    <span>if</span>(<span>$curr</span>){
        <span>foreach</span>(<span>$curr</span><span>as</span><span>$f</span>){
            <span>if</span>(is_dir(<span>$f</span>)){
                array_push(<span>$result</span>, <span>$f</span>);
                traversing(<span>$f</span>, <span>$result</span>);
            }<span>elseif</span>(strtolower(substr(<span>$f</span>, -<span>4</span>))==<span>'.php'</span>){
                array_push(<span>$result</span>, <span>$f</span>);
            }
        }
    }
}
<span>?></span></span></code>

Copyright statement: This article is an original article by the blogger and may not be reproduced without the permission of the blogger.

The above introduces the use of glob method to traverse all files in a folder, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
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
Previous article:Build a PHP environmentNext article:Build a PHP environment