来自 Flysystem 的 php 资源的 BinaryFileResponse

BinaryFileResponse with php resource from Flysystem

我需要使用 BinaryFileResponse 来正确处理长度为 Headers 和 co 的视频。我还希望用户允许配置其他存储(S3、Dropbox)。 flysystem readStream 方法将 return 一个 resourceBinaryFileResponse 需要一个 string 路径。处理这个问题的最佳方法是什么?先把整个文件下载到服务器直到响应?

您可以为此使用 StreamedResponse class。您必须自己进行一些数据布线,但这非常简单。例如,此代码用于为 PDF 启用 "forced downloads"。

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'],
]);