【发布时间】:2013-09-27 01:06:45
【问题描述】:
我创建了一个 zip 文件。但我没有在我的主机中创建任何文件。我想虚拟地创建一个 zip 文件(即临时内存),然后将其下载。我将如何使用 PHP 执行此过程?
【问题讨论】:
-
您是否查看过 PHP 文档中的 ZipArchive 文档?在 php://output 流中创建文件?
我创建了一个 zip 文件。但我没有在我的主机中创建任何文件。我想虚拟地创建一个 zip 文件(即临时内存),然后将其下载。我将如何使用 PHP 执行此过程?
【问题讨论】:
引用nettle that answered this in a previoues question
我遇到了同样的问题,但最终找到了一个有点晦涩的解决方案 并决定在这里分享。
我遇到了很棒的 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();
此脚本允许下载 zip,无需 需要将文件作为真实文件或将 zip 本身保存为 文件。
遗憾的是,此功能不是更通用的功能的一部分 PHP 库。
这里是来自 phpmyadmin 源的 zip.lib.php 文件的链接: https://github.com/phpmyadmin/phpmyadmin/blob/master/libraries/zip.lib.php
确保从开头删除以下检查 zip.lib.php 否则脚本将终止:
if (! defined('PHPMYADMIN')) {
exit;
}
【讨论】: