라이브러리

[PHP] SplFileInfo::getPath - 파일 이름 없이 경로를 가져옵니다.




SplFileInfo::getPath


PHP의 `SplFileInfo` 클래스는 파일 시스템에 대한 정보를 제공하는 클래스입니다. `getPath` 메서드는 파일의 경로를 반환하는 메서드입니다.

# 사용법


`getPath` 메서드는 파일의 경로를 문자열로 반환합니다. 이 메서드는 파일의 절대 경로를 반환하므로, 상대 경로를 사용할 때는 주의해야 합니다.

# 예제


#hostingforum.kr
php

// 파일 경로를 지정합니다.

$file = new SplFileInfo('example.txt');



// getPath 메서드를 사용하여 파일의 경로를 반환합니다.

echo $file->getPath(); // /var/www/html/example.txt



// 상대 경로를 사용할 때는 주의해야 합니다.

$file = new SplFileInfo('./example.txt');

echo $file->getPath(); // /var/www/html/example.txt (Linux) 또는 C:xampphtdocsexample.txt (Windows)



# 예제 2 - 절대 경로와 상대 경로의 차이


#hostingforum.kr
php

// 절대 경로를 사용합니다.

$file = new SplFileInfo('/var/www/html/example.txt');

echo $file->getPath(); // /var/www/html/example.txt



// 상대 경로를 사용합니다.

$file = new SplFileInfo('./example.txt');

echo $file->getPath(); // /var/www/html/example.txt (Linux) 또는 C:xampphtdocsexample.txt (Windows)



# 예제 3 - 디렉토리 경로


#hostingforum.kr
php

// 디렉토리 경로를 지정합니다.

$dir = new SplFileInfo('/var/www/html');

echo $dir->getPath(); // /var/www/html



// 디렉토리 내의 파일 경로를 지정합니다.

$file = new SplFileInfo('/var/www/html/example.txt');

echo $file->getPath(); // /var/www/html/example.txt



# 예제 4 - 파일이 존재하지 않는 경우


#hostingforum.kr
php

// 파일이 존재하지 않는 경우 getPath 메서드는 NULL을 반환합니다.

$file = new SplFileInfo('non_existent_file.txt');

echo $file->getPath(); // NULL



# 예제 5 - 파일이 디렉토리인 경우


#hostingforum.kr
php

// 파일이 디렉토리인 경우 getPath 메서드는 디렉토리 경로를 반환합니다.

$dir = new SplFileInfo('/var/www/html');

echo $dir->getPath(); // /var/www/html


  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

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

검색

게시물 검색