<?
/**
# Auth : shkim
# Email : isunnyk@empal.com, idxzone@naver.com
# Start Date : 2003.09
# Last Date : 2004.02.09.Mon
# Description : Dir..

### 특이사항 한가지..
    fileNUMofDIR( $dir ...) 에서... $dir 의 권한이 700 이어도 읽혀진다..  신기하다.. dir()이라는 함수자체가 그런가?  --aa


####### 사용법 #######
    include 'class.sh_dir_cls.inc';
    $cls_D = new Xena_Dir();
    $dir = $cls_D->CurDir;

    $t = $cls_D->fileNUMofDIR( $dir, $opt='num_dir');
    echo '<br>서브 디렉토리 갯수 : ' . $t[xNo_sub_dir];
    echo '<br>서브 파일 갯수 : ' . $t[xNo_sub_file];
    echo '<br>서브 all 갯수 : ' . $t[xNo_sub_all];
    echo '<br>목록들. : ' . $t[zz_OutPut];
    print_r($t); echo '<hr>';

#####
    $name_of_fileDir = 'class';
    $x = $cls_D->xena2_find_fileName($dir, $opt, $name_of_fileDir);
    if( $x < 0 ){
        echo $cls_D->ErrorMsg;
    }else{
        if( is_array($x) ){
            echo "<hr>찾는 단어 : " . $name_of_fileDir . '<br>';
            echo '찾은 갯수 : '. $x[find_No] . '<br>';
            print_r( $x[find_Str] ) ;
        }
    }
####################
**/



class Xena_Dir{
    var $viewDirSubFile_Lists_Sep = "<br>\n";


    var $CurDir;  //현재 디렉토리. 절대경로
    var $ErrorMsg;


    /******* Basic *********************/
    function Xena_Dir($d=''){
        if( ! trim($d)){
            $d = '.'; //현재 디렉토리
        }

        $this->CurDir = realpath($d);  //절대 경로로 바꾸어서.
        if( !  is_dir( $this->CurDir) ){
            $this->ErrorMsg = '[Error] : 디렉토리가 아니거나, 존재하지 않는 디렉토리입니다.';
            return false;
        }

        return true;
    }


   
    function fileNUMofDIR( $dir, $opt='', $viewType=''){
        //이 함수에서 굳이 $dir 를 주는이유는... 이 함수가 재귀함수이기 때문이다.

        //$opt : 파일의 갯수만 리턴할 것인지, 아지면 서브디렉토리의 갯수도 포함할 것인지.
        //$opt : 'num_file', 'num_all' , 'num_dir'

        switch($opt){
            case 'num_all':
            case 'num_file':
            case 'num_dir':
                break;
            default :
                $opt = 'num_all';
                break;
        }//end switch


        $dir = realpath($dir);  //절대 경로로 바꾸어서.
        if( ! is_dir($dir) ){
            $this->ErrorMsg = "<p>[Error]" . " File: " . __FILE__ . " on line: " . __LINE__ . '<br>';
            return  false;
        }

        $nl = $this->viewDirSubFile_Lists_Sep; //개행..br


        #echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[<b>' . $dir. '/ </b> 디렉토리]' . '<p>';
        $no_all = 0;          $no_file = 0;         $no_dir = 0;
        $uuuu = '';
       

        unset($View_all);
        unset($View_file);
        unset($View_dir);
        $d = dir($dir);   //object...
        while( $entry = $d->read() ){
            if( $entry != '.' && $entry != '..' ){ //자기자신, parent..
                $no_all++;  //현재위치에서의...전체 : 파일+디렉

                $dir2 = $dir. '/'. $entry;
                if( ! is_dir($dir2) ){
                    $no_file++; //현재위치에서의... 파일의 갯수..

                    switch($opt){
                        case 'num_all':
                            #echo $dir. '/'. $entry. $nl;
                            $View_all .=  $dir. '/'. $entry. $nl;
                            break;
                        case 'num_dir':
                            break;
                        case 'num_file':
                            #echo $dir. '/'. $entry. $nl;
                            $View_file .= $dir. '/'. $entry. $nl;
                            break;
                    }//end switch

                   
                }else{
                    $no_dir++;  //현재위치에서의...

                    $z = $this->fileNUMofDIR($dir2, $opt , $viewType);
                    $no_all += $z[xNo_sub_all];
                    $no_file += $z[xNo_sub_file];
                    $no_dir += $z[xNo_sub_dir];

                    $uuuu = $uuuu . $z[zz_OutPut];
               

                    switch($opt){
                        case 'num_all':
                            #echo $dir. '/'. $entry. '/'.  $nl;
                            $View_all .=  $dir. '/'. $entry. '/'.  $nl;
                            break;
                        case 'num_dir':
                            #echo $dir. '/'. $entry. '/'.  $nl;
                            $View_dir .= $dir. '/'. $entry. '/'.  $nl;
                            break;
                        case 'num_file':
                            break;
                    }//end switch
                }
            }
        }//while


        #####@@@@@@@@@@@#####
        switch($opt){
            case 'num_all':
                $my_out_put_TT = $View_all;
                break;
            case 'num_file':
                $my_out_put_TT = $View_file;
                break;
            case 'num_dir':
                $my_out_put_TT = $View_dir;
                break;
        }//switch
        if( ! strcmp($viewType, 'view_p') ){
            #echo $my_out_put_TT ;
        }

        $xx = $my_out_put_TT;
        $outPut =  $xx.  $uuuu;
        #####@@@@@@@@@@@#####

        return array('xNo_sub_all'=>$no_all, 'xNo_sub_file'=>$no_file, 'xNo_sub_dir'=>$no_dir, 'zz_OutPut'=>$outPut);
    }//end function fileNUMofDIR
    /******* Basic *********************/



    /******* appended *********************/
    function get_Permission($dir){
        //return : 755, 644
        $file_dir = $dir;

        $f = $file_dir;
        if( ! $f ){
            return false;
        }
        if( ! file_exists($f)){
            echo "<br>[$f]는 존재하지 않습니다.<br>";
            return false;
        }
        $n = fileperms($f);
        $octal = sprintf("%o", $n);  ## 8진수로
            //$octal = base_convert($n, 10, 8); ## 8진수로
            //$octal = decoct($n);  ## 8진수로
        $perm = substr($octal, -3);  ## 755, 644

        return $perm;
    }


    function xena2_find_fileName($dir, $opt, $name_of_fileDir){
        $name_of_fileDir = trim($name_of_fileDir);
        if( ! $name_of_fileDir ){
            $this->ErrorMsg = '<p>[Error] : 찾고자하는 파일명/디렉토리명을 입력하시오.';
            return -1;
        }

        $tmpAry = $this->fileNUMofDIR( $dir, $opt);
        if( $tmpAry[xNo_sub_all] <= 0 ){
            return 0;  //발견하지 못함 . 에러는 아님
        }else{
            $d = realpath($dir);

            $find_No = 0;
            $find_Ary = array();

            $cprMSG = $tmpAry[zz_OutPut];
                $tmp2 = explode( $this->viewDirSubFile_Lists_Sep  , $cprMSG);
                $cnt = count($tmp2);
                for($i=0; $i<$cnt; $i++){
                    $oneLine = $tmp2[$i];
                    $oneLine = ereg_replace("^($d)", '', $oneLine); //find 명령어는 주어진 디렉토리의 하위를 검색하므로.
                    if( $oneLine ){
                        if( ereg("($name_of_fileDir)", $oneLine ) ){ //찾고자 하는 단어가 발견되는가?
                            $find_No++;
                            $find_Ary[] = $tmp2[$i];
                        }
                    }
                    #echo $oneLine . '<br>';
                }//for

                #echo '<hr>$find_No==' . $find_No;
                #print_r($find_Ary);

            if( $find_No == 0 ){
                return 0;  //발견하지 못함 . 에러는 아님
            }else{
                return array('find_No'=>$find_No, 'find_Str'=>$find_Ary);
            }

        }
    }
    /******* appended *********************/

}//end class

?>

Posted by 알 수 없는 사용자
,