天天看點

PHP 檔案夾 生成 html目錄樹

/* 作者:夢行Monxin */
header('Content-Type:text/html;charset=utf-8');
$path='./';
$v=get_dir_to_html($path);
file_put_contents('dir.html',$v);
echo $v;


function get_dir_to_html($path){
<span style="white-space:pre">	</span>$r=scandir($path);
<span style="white-space:pre">	</span>$html='';
<span style="white-space:pre">	</span>$dirs='';
<span style="white-space:pre">	</span>$files='';
<span style="white-space:pre">	</span>$t='';
<span style="white-space:pre">	</span>foreach($r as $v){
<span style="white-space:pre">		</span>if($v=='.' || $v=='..'){continue;}
<span style="white-space:pre">		</span>
<span style="white-space:pre">		</span>$v=iconv('gbk',"UTF-8".'//IGNORE',$v);
<span style="white-space:pre">		</span>if(is_dir($path.$v)){
<span style="white-space:pre">			</span>$t.="\t";
<span style="white-space:pre">			</span>$dirs.=$t.'<li class=dir id="'.trim($path.$v,'./').'"><div class=line><div>'.$v."</div><div></div></div>\r\n".$t.get_dir_to_html($path.$v.'/')."</li>\r\n";<span style="white-space:pre">	</span>
<span style="white-space:pre">		</span>}else{
<span style="white-space:pre">			</span>$temp=explode('.',$v);
<span style="white-space:pre">			</span>$files.=$t.'<li class='.$temp[count($temp)-1].' id="'.trim($path.$v,'./').'"><div class=line><div>'.$v."</div><div></div></div></li>\r\n";;<span style="white-space:pre">	</span>
<span style="white-space:pre">		</span>}
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>if($dirs!='' || $files!=''){$html="<ul>".$dirs.$files."</ul>\r\n";}
<span style="white-space:pre">	</span>return $html;
}
           
最後再後百度"html 格式化",讓它有TAB符,看起來更清晰