get('/stream', function (Request $request, Response $response, array $args) { $tmpfname = tempnam("/tmp"); // write data to tmpfname (eg. create a zip) // ... // TempStream gets the filename as parameter, not // the resource $file_stream = new TempStream($tmpfname); return $response->withBody($file_stream) ->withHeader('Content-Disposition', 'attachment; filename=document.pdf;') ->withHeader('Content-Type', mime_content_type($tmpfname)) ->withHeader('Content-Length', filesize($tmpfname)); })->setOutputBuffering(false);