0

I have created a zip file. But I don't create any file in my hosting. I want to create a zip file virtually (i.e. Temp Memory) then it's to be download. How I will do this process with PHP?

halfer
  • 19,824
  • 17
  • 99
  • 186
Md. Himel Ali
  • 301
  • 2
  • 10
  • 3
    Have you looked at the documentation for [ZipArchive](http://php.net/manual/en/class.ziparchive.php) in the PHP docs? creating the file in the php://output stream? – Mark Baker Sep 21 '13 at 20:31
  • possible duplicate of [Manipulate an Archive in memory with PHP (without creating a temporary file on disk)](http://stackoverflow.com/questions/1189019/manipulate-an-archive-in-memory-with-php-without-creating-a-temporary-file-on-d) – halfer Sep 21 '13 at 21:10

1 Answers1

1

Quoting nettle that answered this in a previoues question

I had the same problem but finally found a somewhat obscure solution and decided to share it here.

I came accross the great zip.lib.php/unzip.lib.php scripts which come with phpmyadmin and are located in the "libraries" directory.

Using zip.lib.php worked as a charm for me:

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(); 

This script allows downloading of a zip, without the need of having the files as real files or saving the zip itself as a file.

It is a shame that this functionality is not part of a more generic PHP library.

Here is a link to the zip.lib.php file from the phpmyadmin source: https://github.com/phpmyadmin/phpmyadmin/blob/master/libraries/zip.lib.php

Make sure you remove the following check from the beginning of zip.lib.php as otherwise the script just terminates:

    if (! defined('PHPMYADMIN')) {
    exit;
}
Community
  • 1
  • 1
Dan-Levi Tømta
  • 796
  • 3
  • 14
  • 29
  • If you believe this question is a duplicate (and it probably is) a good approach is just to add a comment to that effect under the question. Higher-rep users will see that, and may cast a close vote. – halfer Sep 21 '13 at 20:39
  • 1
    Oh my bad i am sorry, will keep this in mind in the future, thank you for poining it out. – Dan-Levi Tømta Oct 02 '13 at 05:04