我正在尝试使用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);
});
答案 0 :(得分:9)
我正在使用来自php的readfile method:
$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;
}
此示例来自上面的链接,也许您可以尝试使用Slim Response对象和WithHeader方法实现这些标头。
{filename}
和[{filename}]
之间的差异是第二种形式,文件名是可选的。
$app->get('/my[/{optional}]/route', function($req, $res, $args) {});
$app->get('/myother/{not_optional}/route', function($req, $res, $args) {});
在此示例中,您可以访问/ my / route,因为不需要参数但您无法访问/ myother / route,因为您需要not_optional参数。
Slim documentation中有更多信息。
$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;
});
此代码适用于我,它是使用Slim 3.3.0
制作的答案 1 :(得分: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
});