
getSubPath 메서드는 서브 디렉토리가 없을 때 null을 반환하는 것은 PHP의 RecursiveDirectoryIterator 클래스의 기본 동작입니다.
이러한 동작을 변경하고 싶다면, RecursiveIteratorIterator의 hasChildren 메서드를 사용하여 서브 디렉토리가 있는지 확인할 수 있습니다.
#hostingforum.kr
php
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('디렉토리 경로'));
foreach ($iterator as $file) {
if ($iterator->hasChildren()) {
$subPath = $file->getSubPath();
// 서브 디렉토리의 경로가 있는 경우에만 처리합니다.
} else {
// 서브 디렉토리가 없는 경우에 처리합니다.
}
}
또는, RecursiveDirectoryIterator의 isDir 메서드를 사용하여 현재 항목이 디렉토리인지 확인할 수 있습니다.
#hostingforum.kr
php
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('디렉토리 경로'));
foreach ($iterator as $file) {
if ($file->isDir()) {
$subPath = $file->getPathname();
// 서브 디렉토리의 경로가 있는 경우에만 처리합니다.
} else {
// 서브 디렉토리가 없는 경우에 처리합니다.
}
}
이러한 방법들은 디렉토리가 비어있을 때 서브 디렉토리의 경로를 반환하는 것을 방지할 수 있습니다.
2025-07-27 00:54