php 遍历指定路径所有目录与文件夹

php5 实现,两种方法:scandir和DirectoryIterator

function scan_all($dir){

    $temp = scandir($dir);

    if(is_array($temp) && count($temp)>2){

        array_shift($temp);

        array_shift($temp);

        foreach($temp as $v){

            $cur_dir=$dir.DIRECTORY_SEPARATOR.$v;

            if(is_dir($cur_dir)){

                echo $v." =>";

                echo "<blockquote>";

                scan_all($cur_dir);

                echo "</blockquote>";

            }else if(is_file($cur_dir)){

                echo "FILE:".$v."<br/>";

            }else{

                echo 'err';

            }

        }

    }

}
$dir = "D:/yourdir/";
scan_all($dir);

 

 

function directoryiterator($path)
{
$iterator = new DirectoryIterator($path);

foreach ($iterator as $fileinfo) {
if(!$fileinfo->isDot())
{
if($fileinfo->isDir())
{
echo $fileinfo.'=><blockquote>';
directoryiterator($path.DIRECTORY_SEPARATOR.$fileinfo);
echo '</blockquote>';
}
else
{
echo $fileinfo->getFilename().'<br/>';
}
}
}
}
$dir = "D:/yourdir/";
directoryiterator($dir);



你可能感兴趣的:(PHP)