반응형
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
)
)
)
반응형
'개발 꿀팁 > PHP' 카테고리의 다른 글
php 생성 PDF에 사용되는 클래스 MPDF (1) | 2022.09.16 |
---|---|
PHP 조작 Excel – PHP Excel 기본 사용법 상세 (0) | 2022.09.15 |
php가 클라이언트가 pc인지 핸드폰인지 판단하는 방법 (0) | 2022.09.15 |
php-fpm 설치 및 작동 (0) | 2022.09.14 |
PHP 시간 초과 처리 요약 (1) | 2022.09.14 |