Home > Article > Backend Development > 递归读取目录结构到数组中并保存目录结构 php生手
递归读取目录结构到数组中并保存目录结构 php新手
<br /><?php<br />$arr_dir = array();<br />$path = './';<br />//递归读取目录结构到数组中并保存目录结构<br />function makeDir($path,&$arr)<br />{<br /> $dir = opendir($path);<br /> while(($file=readdir($dir))!==FALSE)<br /> {<br /> if($file!='.'&&$file!='..')<br /> {<br /> var_dump($file);<br /> if(!is_dir($path.'/'.$file))<br /> {<br /> $arr[] = $file;<br /> }<br /> else<br /> {<br /> <br /> makeDir($path.'/'.$file, $arr[$file]);<br /> }<br /> }<br /> }<br /> closedir($dir);<br />}<br />makeDir($path, $arr_dir);<br />var_dump($arr_dir);<br />?><br />
$p = './';<br />$ite = new RecursiveDirectoryIterator($p);<br />foreach (new RecursiveIteratorIterator($ite) as $filename=>$cur) {<br /> if(is_dir($filename)) continue;<br /> $res[] = $filename;<br />}<br />print_r($res);