PHP: Zip a file stream on the fly on an intermediate server, without storing (too much) data

谁说我不能喝 提交于 2019-12-10 10:35:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!