개발 꿀팁/PHP

아직 등록된 글이 없습니다.새 글 쓰기

Jammie 2022. 9. 15. 14:54
반응형

PHP 순회목록과 문서목록에 대하여 아래에 간단한 클래스를 하나 쓰고 사용 실례를 첨부합니다. 만약 누락 및 오류가 있다면 여러분께서 지적해 주십시오!

<?php
define('DS', DIRECTORY_SEPARATOR);
 
class getDirFile{
 
    //배열 되돌리기
    private $DirArray  = array();
    private $FileArray = array();
    private $DirFileArray = array();
 
    private $Handle,$Dir,$File;
 
    /디렉터리 목록 가져오기
    public function getDir( & $Dir ){
        if( is_dir($Dir) ){
            if( false != ($Handle = opendir($Dir)) ){
                while( false != ($File = readdir($Handle)) ){
                    if( $File!='.' && $File!='..' && !strpos($File,'.') ){
                        $DirArray[] = $File;
                    }
                }
                closedir( $Handle );
            }
        }else{
            $DirArray[] = '[Path]:\''.$Dir.'\' is not a dir or not found!';
        }
        return $DirArray;
    }
 
    //파일 목록 가져오기
    public function getFile( & $Dir ){
        if( is_dir($Dir) ){
            if( false != ($Handle = opendir($Dir)) ) {
                while( false != ($File = readdir($Handle)) ){
                    if( $File!='.' && $File!='..' && strpos($File,'.') ){
                        $FileArray[] = $File;
                    }
                }
                closedir( $Handle );
            }
        }else{
            $FileArray[] = '[Path]:\''.$Dir.'\' is not a dir or not found!';
        }
        return $FileArray;
    }
 
    //디렉터리/ 파일 목록 가져오기
    public function getDirFile( & $Dir ){
        if( is_dir($Dir) ){
            $DirFileArray['DirList'] = $this->getDir( $Dir );
            if( $DirFileArray ){
                foreach( $DirFileArray['DirList'] as $Handle ){
                    $File = $Dir.DS.$Handle;
                    $DirFileArray['FileList'][$Handle] = $this->getFile( $File );
                }
            }
        }else{
            $DirFileArray[] = '[Path]:\''.$Dir.'\' is not a dir or not found!';
        }
        return $DirFileArray;
    }
 
}
?>

1. 디렉터리 목록 가져오기

<?php
$Dir_dir  = './example';
 
$getDirFile = new getDirFile();
$getDir = $getDirFile->getDir( $Dir_dir );
 
print_r($getDir);
?>

표시:

Array
(
    [0] => example_one
    [1] => example_two
)

2.파일 목록 가져오기

<?php
$File_one_dir = './example/example_one';
$File_two_dir = 'E:/Workspace/mycode/getDirFile/example/example_two';
 
$getDirFile = new getDirFile();
$getFile_one = $getDirFile->getFile( $File_one_dir );
$getFile_two = $getDirFile->getFile( $File_two_dir );
 
print_r($getFile_one);
print_r($getFile_two);
?>

표시:

Array
(
    [0] => example.sql
    [1] => example.txt
)
 
Array
(
    [0] => example.php
)

3. 디렉토리/파일 목록 가져오기

<?php
$Dir_dir  = './example';
 
$getDirFile = new getDirFile();
$getDirFile  = $getDirFile->getDirFile( $Dir_dir );
 
print_r($getDirFile);
?>

표시:

Array
(
    [DirList] => Array
        (
            [0] => example_one
            [1] => example_two
        )
 
    [FileList] => Array
        (
            [example_one] => Array
                (
                    [0] => example.sql
                    [1] => example.txt
                )
 
            [example_two] => Array
                (
                    [0] => example.php
                )
 
        )
 
)
반응형