我遇到了同样的问题,但最终找到了一个不太明显的解决方案,决定在这里分享。
我遇到了很棒的zip.lib.PHP
/ unzip.lib.PHP
脚本,它们PHPmyadmin
都位于“库”目录中。
使用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本身保存为文件。
这是zip.lib.PHP
PHPmyadmin源文件的链接:https://github.com/phpmyadmin/phpmyadmin/blob/RELEASE_4_5_5_1/libraries/zip.lib.php
确保从zip.lib.PHP开头删除以下检查,否则脚本将终止:
if (! defined('PHPMYADMIN')) {
exit;
}
此代码在CodeIgniter项目上也可用:https://github.com/patricksavalle/CodeIgniter/blob/439ac3a87a448ae6c2cbae0890c9f672efcae32d/system/helpers/zip_helper.php