php以不同名字下载同一个文件(x-sendfile)
来源:互联网 发布:淘宝宝贝描述怎么提高 编辑:程序博客网 时间:2024/05/22 07:45
1、linux 下nginx默认支持x-sendfile模式
Nginx 默认支持该特性,不需要加载额外的模块。需要发送的 HTTP 头为 X-Accel-Redirect。另外,需要在配置文件中做以下设定
location /protected/ {
internal;
root /some/path;
}
internal 表示这个路径只能在 Nginx 内部访问,不能用浏览器直接访问防止未授权的下载。
PHP发送 X-Accel-Redirect 给 Nginx:
<?php
$filePath = '/protected/iso.img';
header('Content-type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
//让Xsendfile发送文件
header('X-Accel-Redirect: '.$filePath);
?>
这样用户就会下载到 /some/path/protected/iso.img 这个路径下的文件。
如果你想发送的是 /some/path/iso.img 文件,那么 Nginx 配置应该是
location /protected/ {
internal;
alias /some/path/; # 注意最后的斜杠
}
2、windows下的apache,需要加载module mod_xsendfile
从https://tn123.org/mod_xsendfile/下载Win32 binaries
,放到apache的modules目录下,在http.conf中配置
LoadModule xsendfile_module modules/mod_xsendfile.so
XSendFile On
<?php
$file = "/tmp/dummy.tar.gz";//要用绝对路径,windows下比如d:\tmp\
header("Content-type: application/octet-stream");
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
header("X-Sendfile:".$file);
但是这个有一个问题, 就是如果文件是中文名的话, 有的用户可能下载后的文件名是乱码.
于是, 我们做一下修改(参考: :
<?php$file = "/tmp/中文名.tar.gz";
$filename = basename($file);
header("Content-type: application/octet-stream");
//处理中文文件名
if (strpos($_SERVER["HTTP_USER_AGENT"],"MSIE") === false)
header('Content-Disposition: attachment; filename="' . ($name) . '"');
else
header('Content-Disposition: attachment; filename="' . rawurlencode($name) . '"');
//让Xsendfile发送文件
header("X-Sendfile: $file");
X-Sendfile头将被Apache处理, 并且把响应的文件直接发送给Client.
- php以不同名字下载同一个文件(x-sendfile)
- PHP X-sendfile实现文件下载
- PHP apache X-sendfile扩展实现文件下载
- php nginx用x-sendfile控制文件下载权限
- 使用 Nginx 的 X-Sendfile 机制提升 PHP 文件下载性能
- 使用Nginx的X-Sendfile机制提升PHP文件下载性能
- Yii 同一个表,同一个字段,以不同的名字显示 Multiple Labels in a single Model
- Yii sendFile文件下载设置
- 转:RoR网站如何利用lighttpd的X-sendfile功能提升文件下载性能
- RoR网站如何利用lighttpd的X-sendfile功能提升文件下载性能
- Apache,Nginx,Lighttpd分别使用X-sendfile功能提升文件下载性能
- Apache,Nginx,Lighttpd分别使用X-sendfile功能提升文件下载性能
- 使用 Nginx 的 X-Sendfile 机制控制文件访问权限(php版)
- SpringMvc下载文件,判断不同浏览器的中文名字显示
- 不同浏览器下载文件 中文名字乱码解决
- php 下载名字乱码
- 为什么同一个串口在不同的电脑上名字不一样?
- 以文件流的方式下载文件,以及中文名字转码
- 【JEECG 官方】技术支持联系方式
- 算法导论 4-2
- Web架构“圣经”-------HTTP权威指南--7
- Linux/unix 指令——ps -ef
- php 获取url中文件扩展名
- php以不同名字下载同一个文件(x-sendfile)
- 函数的参数
- UVA - 10192 Vacation
- MatLab矩阵运算——图像灰度化
- JSTL解析——005——core标签库04
- 玩转shell之符号篇
- oracle 获取最近的整点、半点时间
- printf如何输出64位整数
- windows 程序设计 多文档