BinaryFileResponse和来自Flysystem的php资源


BinaryFileResponse with php resource from Flysystem

我需要使用BinaryFileResponse来正确处理带有Length Headers和co的视频。此外,我希望用户允许配置其他存储(S3,Dropbox)。flysystem readStream方法将返回resource,但BinaryFileResponse需要string路径。处理这个问题的最佳方法是什么?首先将整个文件下载到服务器,直到响应?

您可以为此使用StreamedResponse类。你必须自己做一些数据连接,但这是非常直接的。例如,此代码用于启用PDF的"强制下载"。

return new StreamedResponse(function () use ($stream) {
    fpassthru($stream);
    exit();
}, 200, [
    'Content-Transfer-Encoding', 'binary',
    'Content-Type' => 'application/pdf',
    'Content-Disposition' => sprintf('attachment; filename="%s.pdf"', $filename),
    'Content-Length' => fstat($stream)['size'],
]);