当前位置:首页 > 后端 > php > 正文内容

php nginx实现文件下载

hxing6411年前 (2024-04-07)php2676

配置nginx

PHP
server {
        listen        80;
        server_name  demo.com;  #你的域名
        root   "D:/wwwroot/demo";  # 你的项目路径
        # 下载文件关键配置
         location /download/ {
            internal;
            alias G:/files/;  #  文件的路径
            sendfile on;   # 开启高效文件传输模式
            autoindex on;  # 开启目录文件列表
            autoindex_exact_size on;  # 显示出文件的确切大小,单位是bytes
            autoindex_localtime on;  # 显示的文件时间为文件的服务器时间
            charset utf-8,gbk;  # 避免中文乱码
          }
          ...
}

PHP代码实现逻辑

以下代码会下载  G:/files/demo.rar  文件,请确保文件存在

PHP
<?php 
        $origin_name = 'demo.rar';
        // 启用 nginx X-Accel 下载
        header('Content-Type: application/octet-stream');
        $encoded_fname = rawurlencode($origin_name);
        header('Content-Disposition: attachment;filename="'.$encoded_fname.'";filename*=utf-8'."''".$encoded_fname);
        header('X-Accel-Redirect: '. '/download/'. $origin_name);
        header('X-Accel-Buffering: yes');
>

扫描二维码推送至手机访问。

版权声明:本文由星星博客发布,如需转载请注明出处。

本文链接:http://xingxinghan.cn/?id=530

返回列表

上一篇:php实现微信公众号扫码登录

没有最新的文章了...

“php nginx实现文件下载” 的相关文章

php实现微信公众号扫码登录

php实现微信公众号扫码登录

服务器配置登录微信公众平台,左侧点开设置与开发 -> 基本设置,如图所示:设置好按照指示设置好配置,服务端代码如下:<?php // 微信token认证 $signature = $_GET["signature"]; $times...

评论列表

http://www.kangyanshi.com
8个月前 (10-30)

你的文章内容非常卖力,让人点赞。

http://www.jiudong168.com
8个月前 (10-30)

你的文章充满了创意,真是让人惊喜。

https://501h.com/heji/2024-10-30/45787.html

看到你的文章,我仿佛感受到了生活中的美好。

青丝影院
8个月前 (11-17)

《曼谷保镖2(未删减版)》喜剧片无广告高清版:https://www.jinzhuqq.com/dyvideo/37545.html

www.55baobei.com
7个月前 (11-27)

你的文章充满了创意,真是让人惊喜。 https://www.4006400989.com/qyvideo/18260.html

传奇手游
传奇手游 IP:湖北省
7个月前 (11-27)

道术真的能决定宝宝的传奇**走向吗?:https://501h.com/heji/15934.html

传奇私服
传奇私服 IP:湖北省
6个月前 (01-03)

除了直线工具,Word 2013还能用什么工具插入横线?:https://www.tcqinfeng.com/news/223.html

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。