强制使用laravel下载文件的链接

时间:2016-02-23 07:56:31

标签: php laravel laravel-5.1

the link

我希望能够点击链接并从Laravel公用文件夹下载文件。我需要看到我需要编写的路由和控制器来实现这一点。

我能通过php实现这个吗?

if (isset($_GET["download"])){

    $search = $_GET["download"];
    $dir =  $clipDir->clip_path1($search); 
    $Base = basename($dir);

    if (file_exists($dir)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.$Base);
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($dir));
    ob_clean();
    flush();
    readfile($dir);
    exit;
    }  else {
    echo 'File Dont Exit <br />'.  print_r($dir);
    return false;

}

文件存储在Laravels公用文件夹中。

2 个答案:

答案 0 :(得分:1)

查看Response::download并执行以下操作:

return Response::download($yourfile, 'nameoffile.extension');

答案 1 :(得分:0)

您可以直接使用锚标签下载刀片模板中的文件,如下所示

<a href="{{ URL::to( '/uploads/' . $filename)  }}" target="_blank">{{ $filename }}</a>

也适用于Laravel 5.4

相关问题