php以不同名字下载同一个文件(x-sendfile)
2013-09-10 12:43
826 查看
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/下载
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.
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 nginx用x-sendfile控制文件下载权限
- 在PHP中使用X-SendFile头让文件下载更快
- PHP X-sendfile实现文件下载
- PHP X-sendfile实现文件下载
- 在PHP中使用X-SendFile头让文件下载更快
- 使用 Nginx 的 X-Sendfile 机制提升 PHP 文件下载性能
- 使用Nginx的X-Sendfile机制提升PHP文件下载性能
- 使用Nginx的X-Sendfile机制提升PHP文件下载性能
- 在Nginx中使用X-Sendfile头提升PHP文件下载的性能(针对大文件下载)
- RoR网站如何利用lighttpd的X-sendfile功能提升文件下载性能
- pig加载两个不同字段个数的文件?load file with different items(f1有42列,f2有43列读到一个对象中)
- pig加载两个不同字段个数的文件?load file with different items(f1有42列,f2有43列读到一个对象中)
- SpringMvc下载文件,判断不同浏览器的中文名字显示
- 写 5个不同的自己的函数来获取一个全路径的文件的扩展名, 允许封装php 库中已有 的函数(新浪)
- 在ruby 2 和rails 4 里面使用send_file 下载文件的时候。在IE下面中文名出现乱码的处理方式
- 一个简单的PHP文件下载方法 download
- 系统多种资源文件代码示例(例如一个系统同时具有中文简体,中文繁体,英文,日文等不同资源文件)(示例代码下载)
- PHP - Manual手册 - 函数参考 - Filesystem 文件系统函数 - is_file判断给定文件名是否为一个正常的文件
- 系统多种资源文件代码示例(例如一个系统同时具有中文简体,中文繁体,英文,日文等不同资源文件)(示例代码下载)