加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 使用Silex下载文件

发布时间:2020-12-13 21:56:39 所属栏目:PHP教程 来源:网络整理
导读:我的控制器是基本的 $app-get('/files/{type}',function ($type) use ($app) { $path = __DIR__ . "/../files/$type"; if (!file_exists($path)) { $app-abort(404,"Ce fichier n'existe pas."); } return $app -sendFile($path) ;})-bind('getfile'); 根据
我的控制器是基本的

$app->get('/files/{type}',function ($type) use ($app) {

    $path = __DIR__ . "/../files/$type";

    if (!file_exists($path)) {
        $app->abort(404,"Ce fichier n'existe pas.");
    }

    return $app
        ->sendFile($path)
    ;
})->bind('getfile');

根据这doc它的工作原理.当我调用正确的URL时,该文件在当前窗口中打开.

但是我不想在浏览器中打开文件,我想打开对话框来保存文件.

我怎样才能做到这一点 ?

解决方法

您需要将“content-disposition”标头设置为附件.

例如.:

return $app
    ->sendFile($path)
    ->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT,basename($path));

不要忘记在文件顶部添加:

use SymfonyComponentHttpFoundationResponseHeaderBag;

…所以PHP知道ResponseHeaderBag是什么,自动加载器能够找到它.

Linky到docs.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读