giggsey / psr7-stream-response
Build a File Response from a PSR-7 Stream
1.1.0
2024-04-11 07:45 UTC
Requires
- php: >=8.0
- psr/http-message: ^1.0|^2.0
- symfony/http-foundation: ^7.0
README
Why?
Symfony's BinaryFileResponse allows presenting files to download to HTTP Clients. However, this expects full file paths. Some projects may want to stream a PSR-7 Stream to the client instead.
How to use
Instead of returning a BinaryFileResponse, create a PSR7StreamResponse, and return that.
Before
$response = new BinaryFileResponse($filePath); $response = $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, 'my-file.mp3'); return $response;
After
$response = new PSR7StreamResponse($stream, 'audio/mpeg'); $response = $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, 'my-file.mp3'); return $response;