Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BinaryFileResponse with php resource from Flysystem

I need to use BinaryFileResponse for correct handling of videos with Length Headers and co. Also I want the user to allow configured other storages (S3, Dropbox). The flysystem readStream method will return a resource but BinaryFileResponse needs a string path. Whats the best way to handle this? First download the whole file to the server until response?

like image 202
Alexander Schranz Avatar asked Jul 20 '26 08:07

Alexander Schranz


1 Answers

You can use the StreamedResponse class for this. You'll have to do some data wiring yourself but it's pretty straight forward. For example, this code was used to enable "forced downloads" for PDF's.

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'],
]);
like image 182
Frank de Jonge Avatar answered Jul 21 '26 23:07

Frank de Jonge



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!