Skip to content

Instantly share code, notes, and snippets.

@cmpscabral
Last active April 11, 2021 22:39
Show Gist options
  • Save cmpscabral/f1ba21f4ab6d9c26dd16 to your computer and use it in GitHub Desktop.
Save cmpscabral/f1ba21f4ab6d9c26dd16 to your computer and use it in GitHub Desktop.
on the fly zip stream download
<?php
//
// taken from http://stackoverflow.com/questions/4357073/on-the-fly-zipping-streaming-of-large-files-in-php-or-otherwise
//
// make sure to send all headers first
// Content-Type is the most important one (probably)
//
header('Content-Type: application/octet-stream');
header('Content-disposition: attachment; filename="file.zip"');
// use popen to execute a unix command pipeline
// and grab the stdout as a php stream
// (you can use proc_open instead if you need to
// control the input of the pipeline too)
//
$fp = popen('zip -r - file1 file2 file3', 'r');
// pick a bufsize that makes you happy (8192 has been suggested).
$bufsize = 8192;
$buff = '';
while( !feof($fp) ) {
$buff = fread($fp, $bufsize);
echo $buff;
}
pclose($fp);
@amfikuz
Copy link

amfikuz commented Jan 4, 2020

not working
not with small files, not with large in anyway for me :/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment