Created
October 9, 2015 19:22
-
-
Save JoshuaEstes/ea91b9deee94e638ceaf to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<?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