我正在尝试使用 Slim 3 PHP 框架下载文件。Slim 2 相当简单,我确信 Slim 3 也是如此,但我就是不明白。
任何帮助,将不胜感激。基于这里的文档:http ://www.slimframework.com/docs/objects/response.html 我已经从这里添加了包: https ://github.com/guzzle/psr7
所以我的代码此时看起来像:
$app->get('/worksheet/download/{filename}', function ($request, $response, $args) {
error_log("__________________________");
$fileName = $args['filename'];
error_log($fileName);
$newStream = new \GuzzleHttp\Psr7\LazyOpenStream($fileName, 'r');
$newResponse = $response->withHeader('Content-type', 'application/octet-stream')
->withHeader('Content-Description', 'File Transfer')
->withHeader('Content-Disposition', 'attachment; filename=' . basename($fileName))
->withHeader('Content-Transfer-Encoding', 'binary')
->withHeader('Expires', '0')
->withHeader('Cache-Control', 'must-revalidate')
->withHeader('Pragma', 'public')
->withHeader('Content-Length', filesize($fileName))
->withBody($newStream);
return($newResponse);
});