我遇到了同样的问题,但最终找到了一个有点晦涩的解决方案,并决定在这里分享。
我遇到了伟大的zip.lib.php
/unzip.lib.php
附带的脚本phpmyadmin
并位于“libraries”目录中。
Using zip.lib.php
对我来说很有魅力:
require_once(LIBS_DIR . 'zip.lib.php');
...
//create the zip
$zip = new zipfile();
//add files to the zip, passing file contents, not actual files
$zip->addFile($file_content, $file_name);
...
//prepare the proper content type
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=my_archive.zip");
header("Content-Description: Files of an applicant");
//get the zip content and send it back to the browser
echo $zip->file();
此脚本允许下载 zip,而不需要将文件作为真实文件或将 zip 本身保存为文件。
遗憾的是,此功能不是更通用的 PHP 库的一部分。
这是一个链接zip.lib.php
来自 phpmyadmin 源的文件:https://github.com/phpmyadmin/phpmyadmin/blob/RELEASE_4_5_5_1/libraries/zip.lib.php
UPDATE:确保从 zip.lib.php 的开头删除以下检查,否则脚本将终止:
if (! defined('PHPMYADMIN')) {
exit;
}
UPDATE:此代码也可在 CodeIgniter 项目中找到:https://github.com/patricksavalle/CodeIgniter/blob/439ac3a87a448ae6c2cbae0890c9f672efcae32d/system/helpers/zip_helper.php