【问题标题】:How to create a zip file in temp memory, not in my hosting using php then download?如何在临时内存中创建一个 zip 文件,而不是在我的主机中使用 php 然后下载?
【发布时间】:2013-09-27 01:06:45
【问题描述】:

我创建了一个 zip 文件。但我没有在我的主机中创建任何文件。我想虚拟地创建一个 zip 文件(即临时内存),然后将其下载。我将如何使用 PHP 执行此过程?

【问题讨论】:

标签: php mysql


【解决方案1】:

引用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;
}

【讨论】:

  • 如果您认为这个问题是重复的(而且很可能是重复的),那么一个好的方法就是在问题下方添加对此效果的评论。较高代表的用户会看到这一点,并且可能会投下赞成票。
  • 对不起,我很抱歉,以后会记住这一点,谢谢你指出来。
猜你喜欢
  • 2015-11-14
  • 2022-01-12
  • 2010-10-29
  • 1970-01-01
  • 1970-01-01
  • 2018-04-29
  • 2016-02-20
  • 2014-06-30
相关资源
最近更新 更多