2009-06-05 6 views
3

내 웹 사이트는 사용자 정보를 기반으로 3 개의 작은 텍스트 파일을 작성한 다음이 3 개의 파일을 마우스 오른쪽 버튼으로 클릭하여 바탕 화면에 저장해야하는 링크로 제공합니다.PHP Zip 3 작은 텍스트 파일 및 강제 다운로드

나는 이것을 지키고 싶지만, 어떻게 든이 3 개의 작은 파일을 압축하여 다운로드 할 수있는 방법을 제공한다. 그리고 zip 파일도 서버에 저장하고 싶지 않습니다. 이 일을 어떻게 할 수 있습니까?

감사합니다.

답변

12

강제 다운로드의 경우 먼저 파일 헤더를 보내야합니다. 당신은 싶어 하나 PHP의 우편 라이브러리를 사용합니다 압축에 대한

header('content-type: application/zip'); 
header('content-disposition: inline; filename=YOUR_ZIP_FILE_NAME_HERE.ZIP"'); 

는/출력을 압축 된 내용을 에코. 이 같은

http://ca3.php.net/manual/en/zip.examples.php

뭔가 :

$zip = new ZipArchive(); 
//the string "file1" is the name we're assigning the file in the archive 
$zip->addFile(file_get_contents($filepath1), 'file1'); //file 1 that you want compressed 
$zip->addFile(file_get_contents($filepath2), 'file2'); //file 2 that you want compressed 
$zip->addFile(file_get_contents($filepath3), 'file3'); //file 3 that you want compressed 
echo $zip->file(); //this sends the compressed archive to the output buffer instead of writing it to a file. 
+0

실제로 실제로 작성하고 zip 파일을 저장하지 않고 "즉석에서"압축 파일을 생성하는 방법이 있나요? 또는 위의 사항을 수행합니까? –

+0

이 예제는 파일을 저장하지 않고 단지 그것을 에코하기 전에 메모리에 유지합니다. "file1", "file2"및 "file3"은 압축하도록 요청한 파일입니다. – Ian