
DirectoryIterator::getExtension() 메서드는 디렉토리 내의 파일 확장자를 가져올 때 사용됩니다.
디렉토리 내의 특정 확장자를 가진 파일만 가져올 때는 다음과 같이 사용할 수 있습니다.
#hostingforum.kr
php
$iterator = new DirectoryIterator('.');
foreach ($iterator as $file) {
if ($file->getExtension() === 'txt') {
echo $file->getFilename() . "n";
}
}
위 코드는 현재 디렉토리 내의 .txt 확장자를 가진 파일만 가져와 파일 이름을 출력합니다.
또한, 디렉토리 내의 특정 확장자를 가진 파일만 가져올 때는 glob 함수를 사용할 수도 있습니다.
#hostingforum.kr
php
$files = glob('*.txt');
foreach ($files as $file) {
echo $file . "n";
}
위 코드는 현재 디렉토리 내의 .txt 확장자를 가진 파일만 가져와 파일 이름을 출력합니다.
2025-07-01 23:35