从amazon s3通过php服务器流式传输文件

时间:2017-03-21 15:42:27

标签: php amazon-s3 streaming

我想使用PHP将文件从S3流式传输到客户端浏览器。我可以设法做到但我有大文件的问题。首先,它占用会话的所有可用内存,最终出现“内存耗尽”错误。 其次,当第一次到达“fread(...)”时,它会在开始发送到浏览器之前挂起差不多1分钟的50Mo文件。我该如何处理这两个问题?这是我的代码。

$client->register_stream_wrapper();

    if($stream = fopen('s3://' . S3_BUCKET . '/' . $destination_s3, 'r'))
    {
        //Send file to browser.
        header("Cache-Control: no-store, no-cache, must-revalidate");
        header("Cache-Control: post-check=0, pre-check=0", false);
        header("Cache-control: no-store");
        header("Pragma: no-cache");
        header("Content-Disposition: attachment; filename=$file_name");
        header("Content-Type: " . $file_type);
        while (!feof($stream)) {
            echo fread($stream, 1024);
            flush();
        }
        fclose($stream);
    }

0 个答案:

没有答案