2009-06-05 2 views
3

私のウェブサイトはユーザー情報に基づいて3つの小さなテキストファイルを作成し、これらの3つのファイルを右クリックしてデスクトップに保存するリンクとして表示します。PHP Zip 3小さなテキストファイルと強制ダウンロード

私はそれを維持したいと思いますが、何とかこれらの3つの小さなファイルを圧縮してダウンロードする方法を提供しています。私はzipファイルをサーバーに保存したくありません。これはできますか?

ありがとうございます!

答えて

12

強制ダウンロードでは、まずファイルヘッダーを送信する必要があります。

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

ジッピングでは、1つのPHPの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ファイルを作成し、保存せずに「オンザフライ」ZIPファイルを作成する方法はありますか? またはこれはこれを実行しますか? –

+0

この例では、ファイルを保存せず、エコーする前にメモリに保存しています。 "file1"、 "file2"、および "file3"は、圧縮を要求したファイルです。 – Ian

関連する問題