如何流式传输流?

时间:2011-11-08 17:29:40

标签: php zend-framework stream amazon-s3

是否可以将流从Zend_Service_Amazon_S3(http://framework.zend.com/manual/en/zend.service.amazon.s3.html#zend.service.amazon.s3.streaming)流式传输到浏览器?

我正在寻找像readfile(http://www.php.net/manual/en/function.readfile.php)这样的东西,但是后来我没有输入文件作为输入,我想要一个流。

2 个答案:

答案 0 :(得分:1)

是的,只需将其复制到STDOUT,这是“一个只写的流,允许您以与print()和echo()相同的方式写入输出缓冲机制”({{3 }})。

stream_copy_to_stream($response->getStream(), STDOUT);

答案 1 :(得分:0)

类似的解决方案是使用

fpassthru($response->getStream())

由于enter image description hereSTDOUT仅适用于CLI SAPI。但即使在CLI上,它也无法在这样的上下文中使用

php < script.php

在这种情况下(如果您尝试将STDOUTphp-fpm一起使用),您会因stream_copy_to_stream()而出现以下错误:

PHP Notice:  Use of undefined constant STDOUT - assumed 'STDOUT' in $FILE on line $LINE
PHP Warning:  stream_copy_to_stream() expects parameter 2 to be resource, string given in $FILE on line $LINE

使用fpassthru()

后,此问题就会消失

我没有在AWS上测试过这个,但我很确定它也适用于那里。