我正在尝试使用Slim 3 PHP Framework下载文件.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);
});
Run Code Online (Sandbox Code Playgroud)