[php] PHP에서 ZIP 파일을 생성할 때 파일의 수정일자 설정하기
- 개요
- 파일의 수정일자 설정하기
- 예시 코드
- 참고 자료
1. 개요
PHP를 사용하여 ZIP 파일을 생성할 때, 파일의 수정일자를 지정할 수 있습니다. ZIP 아카이브를 생성할 때 파일의 수정일자를 설정하면, 압축 해제 후 해당 파일의 수정일자가 해당일로 설정됩니다.
2. 파일의 수정일자 설정하기
PHP의 ZipArchive
클래스를 사용하여 ZIP 파일을 생성하고, setMtime()
메서드를 사용하여 파일의 수정일자를 지정할 수 있습니다.
$zip = new ZipArchive();
$zipName = 'example.zip';
if ($zip->open($zipName, ZipArchive::CREATE) === TRUE) {
$fileToZip = 'file.txt';
$zip->addFile($fileToZip);
$zip->setMtime($fileToZip, filemtime($fileToZip)); // 파일의 수정일자 설정
$zip->close();
} else {
echo 'Failed to create zip file';
}
위의 예시 코드에서 setMtime()
메서드를 사용하여 ZIP 파일에 추가하는 각 파일의 수정일자를 설정할 수 있습니다.
3. 예시 코드
아래는 ZIP 파일을 생성하고 파일의 수정일자를 지정하는 PHP 코드 예시입니다.
$zip = new ZipArchive();
$zipName = 'example.zip';
if ($zip->open($zipName, ZipArchive::CREATE) === TRUE) {
$fileToZip = 'file.txt';
$zip->addFile($fileToZip);
$zip->setMtime($fileToZip, filemtime($fileToZip)); // 파일의 수정일자 설정
$zip->close();
} else {
echo 'Failed to create zip file';
}
4. 참고 자료
- PHP 공식 문서: ZipArchive 클래스
- PHP 공식 문서: setMtime 메서드