Home >Backend Development >PHP Tutorial >How to Stream Large Zip Files on LAMP Stack for Faster Downloads?
Creating On-the-Fly Zip Files for Large Files on a LAMP Stack
Problem:
When creating zip files of multiple large files for user download, conventional methods result in significant performance issues during the initial phase due to heavy CPU and disk usage.
Solution Using Streaming:
To address this problem, we can leverage the streaming capabilities of zip by using popen() or proc_open() to execute the zip command pipeline and retrieve the stdout as a PHP stream. By combining this with a web server process, we can stream the zip file to the user as it's being created on the fly.
Implementation Using popen():
<?php // Set necessary headers header('Content-Type: application/zip'); header('Content-disposition: attachment; filename="file.zip"'); // Execute zip command and retrieve stdout as a stream $fp = popen('zip -r - file1 file2 file3', 'r'); // Read and echo data in chunks $bufsize = 65535; $buff = ''; while (!feof($fp)) { $buff = fread($fp, $bufsize); echo $buff; } pclose($fp); ?>
Tips for Optimization:
Use the appropriate content-related headers:
Unfortunately, it's not straightforward to set a header to indicate streaming or unknown content length in this context.
Note on flush():
It's important to note that calling flush() within the read/echo loop may cause issues with large files and slow networks due to Apache's internal output buffer getting overrun. Therefore, it's best to omit flush() calls in the code.
The above is the detailed content of How to Stream Large Zip Files on LAMP Stack for Faster Downloads?. For more information, please follow other related articles on the PHP Chinese website!