
FilesystemIterator::current() 메소드는 항상 디렉토리인지 파일인지 구분할 수 있는 방법이 없습니다. 하지만, is_dir() 함수를 사용하여 디렉토리 여부를 확인할 수 있습니다.
#hostingforum.kr
php
$iterator = new FilesystemIterator('/path/to/directory');
$current = $iterator->current();
if (is_dir($current->getPathname())) {
echo '디렉토리';
} else {
echo '파일';
}
또한, is_file() 함수를 사용하여 파일 여부를 확인할 수도 있습니다.
#hostingforum.kr
php
$iterator = new FilesystemIterator('/path/to/directory');
$current = $iterator->current();
if (is_file($current->getPathname())) {
echo '파일';
} else {
echo '디렉토리';
}
또한, FilesystemIterator를 사용하여 디렉토리 내의 파일과 디렉토리를 순회할 때, 디렉토리가 다시 나타나면 is_dir() 함수를 사용하여 디렉토리 여부를 확인할 수 있습니다.
#hostingforum.kr
php
$iterator = new FilesystemIterator('/path/to/directory');
while ($iterator->valid()) {
$current = $iterator->current();
if (is_dir($current->getPathname())) {
echo '디렉토리';
} else {
echo '파일';
}
$iterator->next();
}
위의 예제는 디렉토리 내의 파일과 디렉토리를 순회하며, 디렉토리 여부를 확인하는 방법을 보여줍니다.
2025-05-04 20:02