라이브러리
[PHP] DirectoryIterator::__toString - 파일 이름을 문자열로 가져오기
DirectoryIterator::__toString
PHP의 DirectoryIterator 클래스는 디렉토리 내의 파일 및 디렉토리를 열거하는 데 사용됩니다. DirectoryIterator::__toString 메서드는 디렉토리 내의 파일 및 디렉토리 목록을 문자열로 반환합니다.
# DirectoryIterator::__toString 메서드의 사용법
DirectoryIterator::__toString 메서드는 디렉토리 내의 파일 및 디렉토리 목록을 문자열로 반환합니다. 이 메서드는 디렉토리 내의 모든 항목을 포함하여 목록을 반환합니다.
# 예제
#hostingforum.kr
php
// 디렉토리 경로를 지정합니다.
$dirPath = '/path/to/directory';
// DirectoryIterator 인스턴스를 생성합니다.
$iterator = new DirectoryIterator($dirPath);
// 디렉토리 내의 파일 및 디렉토리 목록을 문자열로 반환합니다.
$directoryList = $iterator->__toString();
// 결과를 출력합니다.
echo $directoryList;
# 예제 결과
#hostingforum.kr
. .. file1.txt file2.txt dir1 dir2
# 디렉토리 내의 파일 및 디렉토리 목록을 문자열로 반환하는 예제
#hostingforum.kr
php
// 디렉토리 경로를 지정합니다.
$dirPath = '/path/to/directory';
// DirectoryIterator 인스턴스를 생성합니다.
$iterator = new DirectoryIterator($dirPath);
// 디렉토리 내의 파일 및 디렉토리 목록을 문자열로 반환합니다.
$directoryList = '';
foreach ($iterator as $file) {
$directoryList .= $file->getFilename() . ' ';
}
$directoryList = rtrim($directoryList); // trailing space 제거
// 결과를 출력합니다.
echo $directoryList;
# 예제 결과
#hostingforum.kr
file1.txt file2.txt dir1 dir2
# 디렉토리 내의 파일 및 디렉토리 목록을 문자열로 반환하는 예제 (파일 확장자별로 분류)
#hostingforum.kr
php
// 디렉토리 경로를 지정합니다.
$dirPath = '/path/to/directory';
// DirectoryIterator 인스턴스를 생성합니다.
$iterator = new DirectoryIterator($dirPath);
// 디렉토리 내의 파일 및 디렉토리 목록을 문자열로 반환합니다.
$directoryList = '';
foreach ($iterator as $file) {
if ($file->isFile()) {
$directoryList .= $file->getFilename() . ' ';
} elseif ($file->isDir()) {
$directoryList .= $file->getFilename() . ' ';
}
}
$directoryList = rtrim($directoryList); // trailing space 제거
// 결과를 출력합니다.
echo $directoryList;
# 예제 결과
#hostingforum.kr
file1.txt file2.txt dir1 dir2
# 디렉토리 내의 파일 및 디렉토리 목록을 문자열로 반환하는 예제 (파일 확장자별로 분류, 확장자별로 분류)
#hostingforum.kr
php
// 디렉토리 경로를 지정합니다.
$dirPath = '/path/to/directory';
// DirectoryIterator 인스턴스를 생성합니다.
$iterator = new DirectoryIterator($dirPath);
// 디렉토리 내의 파일 및 디렉토리 목록을 문자열로 반환합니다.
$directoryList = array();
foreach ($iterator as $file) {
if ($file->isFile()) {
$extension = pathinfo($file->getFilename(), PATHINFO_EXTENSION);
if (!isset($directoryList[$extension])) {
$directoryList[$extension] = array();
}
$directoryList[$extension][] = $file->getFilename();
} elseif ($file->isDir()) {
$directoryList['dir'][] = $file->getFilename();
}
}
// 결과를 출력합니다.
foreach ($directoryList as $extension => $files) {
echo "$extension: ";
echo implode(' ', $files);
echo "
";
}
# 예제 결과
#hostingforum.kr
txt: file1.txt file2.txt
dir: dir1 dir2
-
- 나우호스팅 @pcs8404
-
호스팅포럼 화이팅!
댓글목록
등록된 댓글이 없습니다.