【发布时间】:2016-06-29 21:32:09
【问题描述】:
我正在尝试使用 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);
});
【问题讨论】:
-
应该注意,我尝试下载的文件位于文档根目录之外。否则我只会直接链接到文件。