라이브러리

[PHP] SplFileInfo::getExtension - 파일 확장자를 가져옵니다.




PHP의 SplFileInfo::getExtension


PHP의 `SplFileInfo` 클래스는 파일 정보를 나타내는 클래스입니다. 이 클래스는 파일의 이름, 경로, 크기, 타입, 권한, 소유자, 그룹, 시간 등 다양한 정보를 제공합니다.

`SplFileInfo::getExtension` 메소드는 파일의 확장자를 반환합니다. 예를 들어, `example.txt` 파일의 확장자는 `txt`입니다.

예제


#hostingforum.kr
php

// 파일 경로

$file_path = 'example.txt';



// SplFileInfo 객체 생성

$file_info = new SplFileInfo($file_path);



// 파일 확장자 얻기

$extension = $file_info->getExtension();



// 결과 출력

echo "파일 확장자: $extension";



결과


#hostingforum.kr


파일 확장자: txt



SplFileInfo::getExtension 사용 예제


#hostingforum.kr
php

// 파일 경로

$file_path = array('example.txt', 'example.jpg', 'example.pdf');



// SplFileInfo 객체 생성

$file_info = array();



foreach ($file_path as $file) {

    $file_info[] = new SplFileInfo($file);

}



// 파일 확장자 얻기

foreach ($file_info as $info) {

    $extension = $info->getExtension();

    echo "파일 이름: $info->getFilename(), 확장자: $extension
";

}



결과


#hostingforum.kr


파일 이름: example.txt, 확장자: txt

파일 이름: example.jpg, 확장자: jpg

파일 이름: example.pdf, 확장자: pdf



SplFileInfo::getExtension 사용 시 주의사항


* 파일이 존재하지 않는 경우 `SplFileInfo` 객체를 생성할 수 없습니다. 이 경우 `SplFileInfo` 객체를 생성할 때 예외가 발생합니다.
* 파일이 경로에 따라 다르게 확장자가 지정된 경우, `SplFileInfo::getExtension` 메소드는 파일의 확장자를 반환합니다. 예를 들어, `example.tar.gz` 파일의 확장자는 `gz`입니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

  • 전체 8,985건 / 145 페이지

검색

게시물 검색