laravel和nginx下载文件

时间:2017-08-19 04:37:32

标签: php laravel nginx

laravel和nginx下载文件,我试试这样:

TestController.php,将变量传递给视图:

public function test()
{
    $filepath = '2017/demo.zip';
    $filename = 'demo.zip';

    return view('download', compact('filepath','filename'));
}

download.blade.php,接收变量:

<?php
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename= {{$filename}}");
header('X-Accel-Redirect: /down/'. {{$filepath}} );
exit;
?>

问题:
1,download.blade.php的内容是否正确?或者这个文件没有必要?
2,如果需要download.blade.php,但无法解析{{ }}中的变量,如何编写它?

1 个答案:

答案 0 :(得分:1)

无需渲染视图。你可以使用laravel response helper:

return response()->download(public_path('path_to_directory/demo.zip'));