Last active
April 11, 2021 22:39
-
-
Save cmpscabral/f1ba21f4ab6d9c26dd16 to your computer and use it in GitHub Desktop.
on the fly zip stream download
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
not working
not with small files, not with large in anyway for me :/