问题
Related:
- On-the-fly zipping & streaming of large files, in PHP or otherwise
- Streaming a large file using PHP
I'm looking for a combination of methods described in the other topics. I probably need to read the file (from URL) in small chunks, pipe these into the STDIN of a proc_open zip command, grab output and flush this towards the client.
What I need to do:
- Read a file stream from an URL from a storage server
- Zip it on the fly on a webserver
- Offer it as a download to the web browser, using content-disposition with unknown size (it's a stream after all).
What I can't do:
- Store files on the webserver
- Run code on the storage server
- Use a lot of memory
- Let the client wait for the download
So the flushing of data needs to start while remote chunk-reading is still in progress.
Would this be possible in PHP? I feel like this would be much easier in languages which offer more callback/async functionality, but I'm stuck with PHP in this situation.
Any examples or some rough code would be very welcome!
回答1:
It's not PHP, but this project does exactly what you're looking for. You can host it as a standalone server and call it from php:
https://github.com/scosman/zipstreamer
来源:https://stackoverflow.com/questions/29699777/php-zip-a-file-stream-on-the-fly-on-an-intermediate-server-without-storing-to