Skip to content

Instantly share code, notes, and snippets.

@rela589n
Last active June 24, 2023 08:17
Show Gist options
  • Save rela589n/36f87df1c86a71f8c453b8d9a2409f99 to your computer and use it in GitHub Desktop.
Save rela589n/36f87df1c86a71f8c453b8d9a2409f99 to your computer and use it in GitHub Desktop.
Streaming file response
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