Skip to content

giggsey/psr7-stream-response

Folders and files

NameName
Last commit message
Last commit date

Latest commit

3f5041a · Apr 11, 2024

History

10 Commits
Feb 9, 2022
Aug 8, 2018
Aug 8, 2018
Jan 24, 2024
Apr 11, 2024

Repository files navigation

PSR-7 Stream Response

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;