小智 45
我有同样的问题,但最终找到了一个有点模糊的解决方案,并决定在这里分享.
我来到了很棒的zip.lib.php/ unzip.lib.php脚本,phpmyadmin并且位于"libraries"目录中.
使用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();
Run Code Online (Sandbox Code Playgroud)
此脚本允许下载zip,而无需将文件作为真实文件或将zip本身保存为文件.
遗憾的是,此功能不是更通用的PHP库的一部分.
这是zip.lib.phpphpmyadmin源文件的链接:
https://github.com/phpmyadmin/phpmyadmin/blob/RELEASE_4_5_5_1/libraries/zip.lib.php
更新: 确保从zip.lib.php的开头删除以下检查,否则脚本将终止:
if (! defined('PHPMYADMIN')) {
exit;
}
Run Code Online (Sandbox Code Playgroud)
更新: 此代码也可在CodeIgniter项目中使用: https://github.com/patricksavalle/CodeIgniter/blob/439ac3a87a448ae6c2cbae0890c9f672efcae32d/system/helpers/zip_helper.php