Skip to content

Instantly share code, notes, and snippets.

@JoshuaEstes
Created October 9, 2015 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshuaEstes/ea91b9deee94e638ceaf to your computer and use it in GitHub Desktop.
Save JoshuaEstes/ea91b9deee94e638ceaf to your computer and use it in GitHub Desktop.
<?php
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpFoundation\File\File;
public function streamAction()
{
$file = new File('/path/to/largefile.ext');
// in case you need the container
$container = $this->container;
$response = new StreamedResponse(function() use($container, $file) {
$handle = fopen($file->getRealPath(), 'r');
while (!feof($handle)) {
$buffer = fread($handle, 1024);
echo $buffer;
flush();
}
fclose($handle);
});
$response->headers->set('Content-Type', $file->getMimeType());
return $response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment