Created
January 10, 2018 06:05
-
-
Save james2doyle/5d5e16e4e3b730b78c1160ee23e9c3a2 to your computer and use it in GitHub Desktop.
Create a streaming download of a large file with Slim PHP using the build in Stream class
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
<?php | |
use Slim\Http\Request; | |
use Slim\Http\Response; | |
use Slim\Http\Stream; | |
$app->get('/stream', function (Request $request, Response $response, array $args) { | |
// a 100mb file | |
$path = '../public/files/document.pdf'; | |
$fh = fopen($path, 'rb'); | |
$file_stream = new Stream($fh); | |
return $response->withBody($file_stream) | |
->withHeader('Content-Disposition', 'attachment; filename=document.pdf;') | |
->withHeader('Content-Type', mime_content_type($path)) | |
->withHeader('Content-Length', filesize($path)); | |
})->setOutputBuffering(false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you, you saved my day!