PHP对文件操作的函数很丰富,有打开有读取,但是对文件夹的遍历确是没有的。在网上看到一个笔试题目,要求写出一个能够遍历文件夹的函数,正好今天看了看文件操作,写下一个函数。

<?PHP
    function getDir($dir){
        static $string = '';
        if(is_file($dir)){
            $string.= $dir;
        }else{
            $oDir = @opendir($dir);
            while($fileName = readdir($oDir)){
                if($fileName!='.' && $fileName!='..'){
                    if(is_file($dir.'/'.$fileName)){
                        $string.=$fileName."\n";
                        
                    }elseif(is_dir($dir.'/'.$fileName)){
                        getDir($dir.'/'.$fileName);
                    }
                }
            }
        }
        return $string;
    }
    echo getDir('php');

?>

效果还不错,可以一试~~~

相关文章:

  • 2021-08-10
  • 2021-07-08
  • 2021-09-03
  • 2021-10-10
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-03-08
猜你喜欢
  • 2022-01-20
  • 2021-09-13
  • 2021-08-21
  • 2021-10-13
  • 2022-01-03
  • 2022-12-23
  • 2022-02-17
相关资源
相似解决方案