Skip to content

Instantly share code, notes, and snippets.

@odixon
Forked from james2doyle/slim-stream-route.php
Created July 9, 2019 22:32
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 odixon/db0870eebd42d8a03e67fc094100ffc8 to your computer and use it in GitHub Desktop.
Save odixon/db0870eebd42d8a03e67fc094100ffc8 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
<?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