
locateName 함수의 반환값은 절대 경로로 반환되기 때문에, 파일 이름만 반환하고 싶다면 절대 경로를 제거해야 합니다.
이때, ZipArchive 클래스의 getArchiveName 메서드를 사용하여 zip파일의 이름을 얻은 후, locateName 함수의 반환값에서 zip파일 이름을 제거하면 됩니다.
아래 예제를 참고하세요.
#hostingforum.kr
php
$zipFile = 'example.zip';
$zip = new ZipArchive;
if ($zip->open($zipFile) === TRUE) {
$filePath = $zip->locateName('test.txt');
$fileName = basename($filePath, $zipFile);
$zip->close();
echo $fileName; // test.txt
} else {
echo 'zip파일을 열 수 없습니다.';
}
위 예제에서 basename 함수를 사용하여 zip파일 이름을 제거합니다.
또는, explode 함수를 사용하여 zip파일 이름을 제거할 수도 있습니다.
#hostingforum.kr
php
$filePath = $zip->locateName('test.txt');
$fileName = explode('/', $filePath);
$fileName = end($fileName);
echo $fileName; // test.txt
위 예제에서 explode 함수를 사용하여 zip파일 이름을 제거합니다.
위 두 예제 모두 zip파일 이름을 제거하여 파일 이름만 반환합니다.
위 예제는 PHP에서만 작동합니다.
2025-06-07 21:30