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

Two functions for php to recursively traverse directories

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-25 09:00:24
Original
1137 people have browsed it
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__)));
?>
Copy after login


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template