Last active
June 24, 2023 08:17
-
-
Save rela589n/36f87df1c86a71f8c453b8d9a2409f99 to your computer and use it in GitHub Desktop.
Streaming file response
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function response($path, $name = null, array $headers = [], $disposition = 'inline') | |
{ | |
$response = new StreamedResponse; | |
$filename = $name ?? basename($path); | |
$disposition = $response->headers->makeDisposition( | |
$disposition, $filename, $this->fallbackName($filename) | |
); | |
$response->headers->replace($headers + [ | |
'Content-Type' => $this->mimeType($path), | |
'Content-Length' => $this->size($path), | |
'Content-Disposition' => $disposition, | |
]); | |
$response->setCallback(function () use ($path) { | |
$stream = $this->readStream($path); | |
fpassthru($stream); | |
fclose($stream); | |
}); | |
return $response; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment