PHP Zip 3小文本文件和强制下载

PHP Zip 3小文本文件和强制下载,php,zip,stream,Php,Zip,Stream,我的网站根据用户信息编写3个小文本文件,然后将这3个文件显示为链接,用户必须“右键单击”并保存到桌面 我想保留这一点,但也提供了一种方法来压缩这3个小文件并强制下载。我也不想把zip文件保存在服务器上。这能做到吗?如何做到 谢谢 对于强制下载,您需要首先发送文件头 header('content-type: application/zip'); header('content-disposition: inline; filename=YOUR_ZIP_FILE_NAME_HERE.ZIP"')

我的网站根据用户信息编写3个小文本文件,然后将这3个文件显示为链接,用户必须“右键单击”并保存到桌面

我想保留这一点,但也提供了一种方法来压缩这3个小文件并强制下载。我也不想把zip文件保存在服务器上。这能做到吗?如何做到


谢谢

对于强制下载,您需要首先发送文件头

header('content-type: application/zip');
header('content-disposition: inline; filename=YOUR_ZIP_FILE_NAME_HERE.ZIP"');
对于压缩,您需要使用一个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.

有没有一种方法可以在不实际创建和保存压缩文件的情况下“动态”创建压缩文件?或者上述方法可以做到这一点吗?这个例子可以做到这一点。。它不保存文件,只是在回显之前将其保存在内存中。“file1”、“file2”和“file3”是您要求压缩的文件。