LAMP:如何从远程URL/文件创建.Zip并将其即时流传输到客户端 [英] LAMP: How to create .Zip from remote URLs/files and stream it to the client on the fly

查看:146
本文介绍了LAMP:如何从远程URL/文件创建.Zip并将其即时流传输到客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

借助诸如S3的外部服务来存储对象,提供存储文件的自定义zip存档的旧问题变得更加复杂.一种方法是,Web服务器将所有资源复制到临时本地文件中,将它们编译为.zip,然后将zip返回给用户,但这很慢且占用大量资源.

With external services like S3 for object storage, the old problem of providing custom zip archives of stored files gets a bit more complicated. One way would be for a web server to copy all the resources to temporary local files, compile them into a .zip, then return the zip to the user, but this is slow and resource intensive.

是否可以类似于

Can this be done similarly to the solution for local files? e.g. can you curl the files, pipe them into zip in streaming mode, then out to the user on the fly?

推荐答案

是的-您可以让zip fifos集进行操作,而不是对一组文件进行操作.这是代码:

Yes — you can have zip operate on a set of fifos rather than a set of files. Here’s the code:

$sh = 'mkfifo a.jpg b.jpg ; '.                     // 1
  'curl -s some_service.com/a.jpg > a.jpg & ' .    // 2
  'curl -s some_service.com/b.jpg > b.jpg & ' .    // 
  'zip -FI - a.jpg b.jpg | cat > out.fifo';        // 3

// Open output fifo & curl-zip task pipe
posix_mkfifo('out.fifo', 0777);                    // 4
$pipe_output = popen('cat out.fifo', 'r');         //
$pipe_curlzip = popen($sh, 'r');                   //

它的工作原理如下:

  1. 我们有两个远程文件:some_service.com/a.jpgsome_service.com/b.jpg –我们为每个文件创建一个fifo,每个fifo都具有相应文件的名称,并希望它出现在zip文件中.因此,对于some_service.com/a.jpg,我们创建一个名为a.jpg的fifo.
  2. 启动两个curl任务,以将远程文件a.jpgb.jpg馈入各自的FIFO中.这些curl任务将一直挂起,直到从其输出fifo中读取某些内容为止,因此我们在后台(&)中运行它们.
  3. 启动我们的zip任务,为其输入两个fifo作为输入.我们将其流输出cat third fifo – out.fifo –中,以允许我们的PHP进程将文件流传输给用户.
  4. 开始吧.
  1. We have two remote files: some_service.com/a.jpg and some_service.com/b.jpg – we create a fifo for each, each fifo having the name of the corresponding file as we wish it to appear in our zip file. So for some_service.com/a.jpg, we create a fifo called a.jpg.
  2. Kick off two curl tasks to feed the remote files a.jpg and b.jpg into each’s respective fifo. These curl tasks will hang until something reads from their output fifos, so we run them in the background (&).
  3. Start our zip task, giving it our two fifos as input. We cat its streaming output into a third fifo – out.fifo – to allow our PHP process to stream the file to the user.
  4. Kick things off.

然后只需阅读out.fifo的内容并将其发送给用户:

It's then just a matter of reading what comes out of out.fifo and sending it to the user:

header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="Download.zip"');

while (!feof($pipe_output)) {
    echo fread($pipe_output, 128);   // Your magic number here
}

这篇关于LAMP:如何从远程URL/文件创建.Zip并将其即时流传输到客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆