php如何下载文件
来源:互联网 发布:怎么用sql语句降序排列 编辑:程序博客网 时间:2024/05/16 15:46
X-SendFile in Apache2
Normally when want to let a user download a file, you simply stick it in a dir under the document root and let Apache do the rest.
Let PHP serve the download
However in some cases that is not good enough. You might need to do some authenticate first or you need to lookup the actual file name. In that case you would use PHP, which would result in a script looking like this:
123456789
authenticate(); # authenticate and authorize, redirect/exit if failed$file = determine_file(); if (!file_exists($file)) trigger_error("File '$file' doesn't exist.", E_USER_ERROR); header("Content-type: application/octet-stream");header('Content-Disposition: attachment; filename="' . basename($file) . '"');header("Content-Length: ". filesize($file));readfile($file);
This means PHP has to read in the file, which goes through the output buffer, is flushed to Apache and processed before send to client. If I want to make the caching based on last-modified work, you need to check the if-modified-since request header, check the mtime of the file and send a 304 result header.
X-SendFile to the rescue
Wouldn’t it be nicer to tell Apache, please send that file, and be done with it. Well, you can. When you enable Apache module ‘mod_xsendfile’ in Apache, you can send an X-SendFile header, which is processed by Apache.
123456
authenticate(); # authenticate and authorize, redirect/exit if failed$file = determine_file(); header("X-Sendfile: $file");header("Content-type: application/octet-stream");header('Content-Disposition: attachment; filename="' . basename($file) . '"');
Note that this technique was copied from Lighttp. NGinx supports an alternative header called X-Accel-Redirect.
原文:http://blog.jasny.net/articles/how-i-php-x-sendfile/
- php如何下载文件
- php如何实现文件下载
- 如何使用PHP来强制文件下载
- PHP中,如何创建文件下载
- PHP实现文件下载
- PHP实现文件下载
- PHP实现文件下载
- php 文件下载类
- php自动下载文件
- php下载文件代码
- PHP实现文件下载
- 关于PHP文件下载
- PHP强制性文件下载
- php 下载文件代码
- PHP下载文件
- php下载文件
- php强制下载文件
- php下载文件
- Android Layout含义
- Java学习入门
- sqlserver2008安装以及远程登录配置
- SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)
- Uva 10976
- php如何下载文件
- JDK源码解析集合篇--ArrayList全解析
- Linux复用I/O-epoll-server代码
- 《并发编程》--14.Java线程池和自定义线程
- 前端性能优化--yahoo 军规
- 洛谷p1005矩阵取数游戏
- 莫名的一个mysql耗时记录
- 运算符
- thinkPHP5-分页的实现