Home  >  Article  >  Backend Development  >  递归读取目录结构到数组中并保存目录结构 php生手

递归读取目录结构到数组中并保存目录结构 php生手

WBOY
WBOYOriginal
2016-06-13 11:52:591339browse

递归读取目录结构到数组中并保存目录结构 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);

Statement:
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