您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

使用PHP处理内存中的存档(无需在磁盘上创建临时文件)

使用PHP处理内存中的存档(无需在磁盘上创建临时文件)

我遇到了同样的问题,但最终找到了一个不太明显的解决方案,决定在这里分享

我遇到了很棒的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本身保存为文件

遗憾的是,此功能不是更通用的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;
}

代码在CodeIgniter项目上也可用:https://github.com/patricksavalle/CodeIgniter/blob/439ac3a87a448ae6c2cbae0890c9f672efcae32d/system/helpers/zip_helper.php

php 2022/1/1 18:15:31 有459人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶