Home >Backend Development >PHP Tutorial >Two functions for php to recursively traverse directories

Two functions for php to recursively traverse directories

WBOY
WBOYOriginal
2016-07-25 09:00:241128browse
The two functions for recursively traversing directories implemented in PHP use functions such as glob, is_dir, and dir in PHP. Friends in need can refer to them.

Achieved with the help of PHP built-in functions glob, is_dir, and dir.

<?php
/**
 * 函数1
 * 递归遍历目录
 * site bbs.it-home.org
*/
    function myscandir($pathname){
        foreach( glob($pathname) as $filename ){
            if(is_dir($filename)){
                myscandir($filename.'/*');
            }else{
                echo $filename.'<br/>';
            }
        }
    }
    myscandir('D:/wamp/www/exe1/*');

/**
 * 函数2
 * 使用dir()函数
*/
    function myscandir($path){
        $mydir=dir($path);
        while($file=$mydir->read()){
            $p=$path.'/'.$file;
       
            if(($file!=".") AND ($file!="..")){
            echo $p.'<br>';
            }
       
            if((is_dir($p)) AND ($file!=".") AND ($file!="..")){
                myscandir($p);
            }
        }   
    }
    myscandir(dirname(dirname(__FILE__)));
?>


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