dongqi0644 2016-02-23 07:56
浏览 98

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

the link

I want to be able to click on a link and it downloads a file from Laravel public folder. I need to see the routes and controller that I need to write to achieve this.

Could I achieve this via php only?

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;

}

Files are stored in Laravels public folder .

  • 写回答

2条回答 默认 最新

  • dsvs50005 2016-02-23 07:58
    关注

    Take a look at Response::download and do something like:

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

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记