PHP遍历目录文件

public function read_all_dir($dir ='')
    {
        $result = array();
//      $dir = "../uploads/";
        if (is_dir($dir))
        {
            if ($dh = opendir($dir)) {
                while (($file = readdir($dh)) !== false) {
                    if ($file != '.' && $file != '..'){
                        $cur_path = $dir . DIRECTORY_SEPARATOR . $file;
                        $cur_path = iconv("gb2312","UTF-8", $cur_path);//存在中文转码
                        if (is_dir($cur_path )) {//判断是否为目录,递归读取文件
                            $result['dir'][$cur_path] = $this->read_all_dir($cur_path );
                        }else{
                            $result['file'][] = $cur_path;
                        }
                    }
                }
                closedir($dh);
            }
        }
        return $result;
    }

猜你喜欢

转载自blog.csdn.net/amberom/article/details/81029338