Jim*_*m_M 8 php download slim slim-3
我正在尝试使用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)
leg*_*ina 11
我正在使用php的readfile方法:
$file = 'monkey.gif';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment;filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
Run Code Online (Sandbox Code Playgroud)
此示例来自上面的链接,也许您可以尝试使用Slim Response对象和WithHeader方法实现这些标头.
{filename}和之间的差异[{filename}]是第二种形式,文件名是可选的.
$app->get('/my[/{optional}]/route', function($req, $res, $args) {});
$app->get('/myother/{not_optional}/route', function($req, $res, $args) {});
Run Code Online (Sandbox Code Playgroud)
在此示例中,您可以访问/ my/route,因为不需要参数但您无法访问/ myother/route,因为您需要not_optional参数.Slim文档中
有更多信息.
$app->get('/download', function($req, $res, $args) {
$file = 'public_html/images/back2.jpg';
$response = $res->withHeader('Content-Description', 'File Transfer')
->withHeader('Content-Type', 'application/octet-stream')
->withHeader('Content-Disposition', 'attachment;filename="'.basename($file).'"')
->withHeader('Expires', '0')
->withHeader('Cache-Control', 'must-revalidate')
->withHeader('Pragma', 'public')
->withHeader('Content-Length', filesize($file));
readfile($file);
return $response;
});
Run Code Online (Sandbox Code Playgroud)
这段代码对我来说很好用,它是用Slim 3.3.0制作的
小智 5
$app->get('/test-download', function($request, Slim\Http\Response $response, $args) {
$file = __DIR__ . '/2.csv';
$fh = fopen($file, 'rb');
$stream = new \Slim\Http\Stream($fh); // create a stream instance for the response body
return $response->withHeader('Content-Type', 'application/force-download')
->withHeader('Content-Type', 'application/octet-stream')
->withHeader('Content-Type', 'application/download')
->withHeader('Content-Description', 'File Transfer')
->withHeader('Content-Transfer-Encoding', 'binary')
->withHeader('Content-Disposition', 'attachment; filename="' . basename($file) . '"')
->withHeader('Expires', '0')
->withHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0')
->withHeader('Pragma', 'public')
->withBody($stream); // all stream contents will be sent to the response
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12182 次 |
| 最近记录: |