php生成zip文件,表格-即时生成PHP ZIP文件

表格-即时生成PHP 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.

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

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

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

对不起,我的基本问题。

asked 2020-07-12T05:43:43Z

6个解决方案

69 votes

不幸的是,在CentOS 5.x上带有PHP 5.3.4-dev和Zend Engine v2.3.0,我无法使上面的代码正常工作。 我只能得到“无效或统一的Zip对象”错误消息。 因此,为了使其正常工作,我不得不使用以下代码段(摘自PHP.net手册上的Jonathan Baltazar的示例,位于ZipArchive :: open页):

// 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);

我知道这与仅使用内存工作有所不同-除非您在ram中有tmp区域;-)-但这可能可以帮助其他人,就像我以前那样,在上述解决方案中苦苦挣扎; 而性能损失不是问题。

maraspin answered 2020-07-12T05:44:03Z

9 votes

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

$zip->addFile(file_get_contents($filepath1), 'file1');

应该

$zip->addFile($filepath1, 'file1');

[HTTP://US3.PHP.net/manual/恩/function.zip archive-add file.PHP]

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

$zip->addFromString( 'file1', $data );

sakabako answered 2020-07-12T05:44:36Z

5 votes

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

$zipped_data = `zip -q - files`;

header('Content-type: application/zip');

header('Content-Disposition: attachment; filename="download.zip"');

echo $zipped_data;

?>

文件是要压缩的内容,并将位置压缩到zip可执行文件中。

当然,这里假设使用Linux或类似操作系统。 我猜,在Windows中,您也许可以使用其他压缩工具执行类似的操作。

这里还有一个zip扩展名,用法。

Vinko Vrsalovic answered 2020-07-12T05:45:09Z

4 votes

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

header('Content-Length: ' . filesize($file));

经过上述更改,代码变得轻而易举!没有此更改,我曾经得到以下错误消息:

找不到中央目录末尾签名。 该文件不是zip文件,或者构成了多部分归档文件的一个磁盘。 在后一种情况下,将在此存档的最后一个磁盘上找到中央目录和zipfile注释。

测试环境:

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

itsols answered 2020-07-12T05:45:47Z

1 votes

固醇如果要插入“ Content-Length”,请按照以下步骤操作:

$length = filesize($file);

header('Content-Length: ' . $length);

我不知道为什么,但是如果将其放在同一行中会崩溃。

Arkiller answered 2020-07-12T05:46:11Z

1 votes

要在内存中动态创建ZIP文件,可以使用phpMyAdmin中的ZipFile类:

PMA \ libraries \ ZipFile.php

如何在自己的应用程序中使用它的示例:

[HTTPS://stack overflow.com/啊/9648562/767871]

注意:您的ZIP文件将受到PHP内存限制的限制,如果超出限制,则会导致存档损坏。

dezlov answered 2020-07-12T05:46:49Z

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值