PHP ZIP文件即时

45 php forms zip

从服务器上的文件夹压缩,说2个文件最简单的方法是什么?强行下载?不保存"zip"到服务器.

    $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.
Run Code Online (Sandbox Code Playgroud)

有人可以验证:我有一个包含test1.doc,test2.doc和test3.doc的文件夹

使用上面的示例 - file1(file2和file3)可能只是test1.doc等.

我必须对"$ filepath1"做任何事情吗?这是包含3个文档的文件夹目录吗?

对不起我的基本问题..

mar*_*pin 71

不幸的是,在CentOS 5.x上使用PHP 5.3.4-dev和Zend Engine v2.3.0我无法获得上面的代码.我无法获得" 无效或单一化的Zip对象 "错误消息.因此,为了使其工作,我不得不使用以下片段(摘自Jonathan Baltazar在PHP.net手册中的示例,在ZipArchive ::打开页面):

// Prepare File
$file = tempnam("tmp", "zip");
$zip = new ZipArchive();
$zip->open($file, ZipArchive::OVERWRITE);

// Stuff with content
$zip->addFromString('file_name_within_archive.ext', $your_string_data);
$zip->addFile('file_on_server.ext', 'second_file_name_within_archive.ext');

// Close and send to users
$zip->close();
header('Content-Type: application/zip');
header('Content-Length: ' . filesize($file));
header('Content-Disposition: attachment; filename="file.zip"');
readfile($file);
unlink($file); 
Run Code Online (Sandbox Code Playgroud)

我知道这与仅使用内存工作不同 - 除非你在ram中有你的tmp区域;-) - 但也许这可以帮助其他人,他正在努力解决上述解决方案,就像我一样; 而且性能损失不是问题.


sak*_*ako 9

你的代码非常接近.您需要使用文件名而不是文件内容.

$zip->addFile(file_get_contents($filepath1), 'file1');
Run Code Online (Sandbox Code Playgroud)

应该

$zip->addFile($filepath1, 'file1');
Run Code Online (Sandbox Code Playgroud)

http://us3.php.net/manual/en/function.ziparchive-addfile.php

如果需要从变量而不是文件添加文件,可以使用addFromString函数.

$zip->addFromString( 'file1', $data );
Run Code Online (Sandbox Code Playgroud)


小智 8

这对我有用(没有任何内容写入磁盘)

$tmp_file = tmpfile(); //temp file in memory
$tmp_location = stream_get_meta_data($tmp_file)['uri']; //"location" of temp file

$zip = new ZipArchive;
$res = $zip->open($tmp_location, ZipArchive::CREATE);
$zip->addFile($filepath1, 'file1');
$zip->close();

header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="download.zip"');
echo(file_get_contents($tmp_location));
Run Code Online (Sandbox Code Playgroud)

  • 您的评论应该编辑到您的答案中。(然后,当然,删除你的评论。) (4认同)
  • `tmpfile` 不在内存中。据我所知,这取决于你设置的温度。默认它位于`/tmp/`路径中。另请参阅:/sf/answers/2630223221/ (3认同)

Vin*_*vic 5

如果您可以访问zip命令行实用程序,则可以尝试

<?php
$zipped_data = `zip -q - files`;
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="download.zip"');
echo $zipped_data;
?>
Run Code Online (Sandbox Code Playgroud)

其中files是要压缩的东西,并将位置压缩到zip可执行文件.

当然,这假设是Linux或类似的.在Windows中,我猜你可以用另一种压缩工具做类似的事情.

还有一个zip扩展,使用显示在这里.


its*_*ols 5

maraspin 的答案是我尝试过的。奇怪的是,我通过删除引用文件大小的标题行来使其工作:

header('Content-Length: ' . filesize($file));
Run Code Online (Sandbox Code Playgroud)

通过上述更改,代码的运行变得轻而易举!如果没有进行此更改,我常常收到以下错误消息:

未找到中央目录结尾签名。该文件要么不是 zip 文件,要么构成多部分存档的一张磁盘。在后一种情况下,中央目录和 zip 文件注释将在此存档的最后一个磁盘上找到。

测试环境:

操作系统:Ubuntu 10.10 浏览器:Firefox 和默认的 LAMP 堆栈。