사이트 내 전체검색
PHP
[php] 재귀호출을 이용한 디렉토리 트리구조
로빈아빠
https://cmd.kr/php/525 URL이 복사되었습니다.

본문

재귀호출을 이용한 디렉토리 트리구조

+ main │ + images ││ + menu ││ + top │ + work

/*

제작자이름 : navyism
제작자메일 : navyism@chol.com
관련사이트 : http://navyism.com

*/

function get_tree($start) 
{ 
$dh=@opendir($start); 

$block_size=@explode("/",$start); 

        for($i=1;$i<sizeof($block_size);$i++) 
        { 
        $block.="<font color=#999999>│</font>"; 
        } 

        unset($buff); 

        while($dir=@readdir($dh)) 
        { 
                if($dir=="." || $dir=="..") 
                { 
                continue; 
                } 

                if(!$dh2=@opendir("$start/$dir")) 
                { 
                continue; 
                } 
         
        $buff[]=$dir;         
        @closedir($dh2); 
        } 

        @sort($buff); 

        for($i=0;$i<sizeof($buff);$i++) 
        {         
        echo $block." <font color=#999999>+</font> $buff[$i]<br>\n"; 
        get_tree("$start/$buff[$i]"); 
        } 

@closedir($dh); 
} 



위 소스를 응용해서 만든것입니다.

첨부파일을 확인하세요

<? 
$start = "./";    //시작위치 
$limit = 50;    //폴더당 최대서브폴더 

function get_tree($start,$depth=0,$root=0) 
{ 
global $limit; 

$dh=@opendir($start); 

    for($i=0;$i<$depth;$i++) 
    { 
    $block.="<img src=blank.gif align=absmiddle border=0>"; 
    } 

    unset($buff); 

        if($depth) 
        { 
        $position="absolute"; 
        $visibility="hidden"; 
        } 
        else 
        { 
        $position=""; 
        $visibility="visible"; 
        } 


    while($dir=@readdir($dh)) 
    { 
        if($dir=="." || $dir=="..") 
        { 
        continue; 
        } 

        if(!$dh2=@opendir("$start/$dir")) 
        { 
        continue; 
        } 
     
    $buff[]=$dir;     
    @closedir($dh2); 
    } 

    @sort($buff); 

        if($i>=$limit) 
        { 
        $buff[$i]="more....."; 
        } 

    for($i=0;$i<sizeof($buff);$i++) 
    {     
    echo "<div open=0 
    style='height:17;position:$position;visibility:$visibility'  
    id='tree_${root}_${i}'>".$block." <a onfocus=this.blur()  
    href=\"javascript:show_div('${root}_${i}')\"><img  
    name='button_${root}_${i}' src=button+.gif align=absmiddle border=0></a> <img  
    name='folder_${root}_${i}' src=folder_close.gif align=absmiddle border=0> <input 
    type=checkbox name='chk_${root}_${i}' onclick=flagging('${root}_${i}',this.checked)> <a onfocus=this.blur()  
    href=\"javascript:show_div('${root}_${i}')\"><font id='font_${root}_${i}' howmany=0>$buff[$i]</font></a><span id='span_${root}_${i}'></span></div>\n"; 
     
        if($i>=$limit) 
        { 
        break; 
        } 

    get_tree("$start/$buff[$i]",$depth+1,$root."_".$i); 
    } 

@closedir($dh); 
} 
?>

<html> 
<body> 

<style> 
A:link    { 
    color:#000000; 
    text-decoration: none; 
} 
A:visited { 
    color:#000000; 
    text-decoration: none; 
} 
A:active  { 
    color:#000000; 
    text-decoration: none; 
} 
A:hover  { 
    color:#0000cc; 
    text-decoration: underline; 
} 

body 
{     
font-size:9pt; 
font-family:tahoma,굴림; 
line-height:145%; 
} 
</style> 

<script language=javascript> 
function ereg(exp,str)  
{  
exp=new RegExp(exp);  
exp=str.match(exp);  
return exp;  
}  

function flagging(id,chk) 
{ 
this_chk=id.split("_"); 
temp_chk="0"; 

    for(var i=1;i<this_chk.length;i++) 
    { 
    temp_chk+="_"+this_chk[i]; 
    span_chk=eval("span_"+temp_chk); 
    font_chk=eval("font_"+temp_chk); 


        if(chk) 
        { 
        font_chk.howmany++; 
        } 
        else 
        { 
        font_chk.howmany--; 
        } 

        if(font_chk.howmany) 
        { 
        font_chk.style.color="#0000cc"; 
        span_chk.innerHTML="<img src=flag.gif align=absmiddle>"; 
        } 
        else 
        { 
        font_chk.style.color="#000000"; 
        span_chk.innerHTML=""; 
        } 
    }     
} 
function show_div(id) 
{ 
this_div=eval("tree_"+id); 
this_btn=eval("document.button_"+id); 
this_fld=eval("document.folder_"+id); 

this_haveson=0; 

    if(this_div.open==0) 
    { 
    this_div.open=1; 

        for(var i=0;i<<?=$limit?>;i++) 
        { 
            try 
            { 
            chld_div=eval("tree_"+id+"_"+i); 
            chld_div.style.position=''; 
            chld_div.style.visibility='visible'; 
            this_haveson=1; 
            } 
            catch(Exception) 
            { 
            break; 
            } 
        } 

        if(this_haveson) 
        { 
        this_btn.src='button-.gif'; 
        this_fld.src='folder_open.gif'; 
        } 
        else 
        { 
        this_btn.src='blank.gif'; 
        this_fld.src='folder_close.gif'; 
        } 
    } 
    else 
    { 
    close_all(id); 
    } 
} 

function close_all(id) 
{ 
this_div=eval("tree_"+id); 
this_div.open=0; 

this_btn=eval("document.button_"+id); 

    if(!ereg("blank\.gif$",this_btn.src)) 
    { 
    this_btn.src='button+.gif'; 
    } 

this_fld=eval("document.folder_"+id); 
this_fld.src='folder_close.gif'; 

    for(var i=0;i<<?=$limit?>;i++) 
    { 
        try 
        { 
        chld_div=eval("tree_"+id+"_"+i); 
        is_ok=1; 
        } 
        catch(Exception) 
        { 
        is_ok=0; 
        } 

        if(!is_ok) 
        { 
        break; 
        } 
        else 
        { 
        chld_div.style.position='absolute'; 
        chld_div.style.visibility='hidden'; 

        chld_btn=eval("document.button_"+id+"_"+i); 

            if(!ereg("blank\.gif$",chld_btn.src)) 
            { 
            chld_btn.src='button+.gif'; 
            } 

        chld_fld=eval("document.folder_"+id+"_"+i); 
        chld_fld.src='folder_close.gif'; 
        close_all(id+"_"+i); 
        } 
    } 
} 
</script> 


<form name=reg> 
<? 
get_tree($start); 
?> 
</form> 

<span id=res></span> 
</body> 
</html>

댓글목록

등록된 댓글이 없습니다.

PHP
871 (9/18P)

Search

Copyright © Cmd 명령어 18.119.113.79