PHP文件下载
2015-11-07 18:39
567 查看
摘要: PHP的文件下载类
<?php class Down { private $file = ''; private $msg = null; public function __construct($file,$buffer=1024) { // 用以解决中文不能显示出来的问题 $file = iconv('utf-8','gbk',$file); $this->file = $_SERVER['DOCUMENT_ROOT'].'/'.$file; $this->buffer = $buffer; } public function act() { if (!file_exists($this->file)) { // 如果需要下载的文件不存在 $this->msg = '文件不存在'; return $this->msg; } $handle = fopen($this->file,'r'); if (!$handle) { // 无法打开文件 $this->msg = '无法打开文件'; return $this->msg; } $filesize = filesize($this->file); // 下载需要的文件头 header('content-type:application/ocet-stream'); header('Accept-Ranges:bytes'); header('Accept-Length:'.$filesize); header('Content-Disposition:attachment;filename='.$this->file); $file_count = 0; // 向浏览器返回数据 while(!feof($handle) && $file_count<$filesize){ $file_down = fread($handle,$this->buffer); $file_count += $this->buffer; echo $file_down; } // 关闭资源 fclose($handle); $msg = '下载完毕'; // return $msg; return $file_down; } } header('content-type:text/html;charset="utf-8"'); header('expire:-1'); header('cache-control:no-cache'); header("pragma:no-cache"); $down = new Down('d:/myblog/test.jpg'); $msg = $down->act(); echo $msg; ?>
相关文章推荐
- 一个关于if else容易迷惑的问题
- RPC failed; result=22, HTTP code = 411
- PHP5.2.*防止Hash冲突拒绝服务攻击的Patch
- 深入理解PHP之匿名函数
- JSP/PHP基于Ajax的分页功能实现
- 关于PHP通过PDO用中文条件查询MySQL的问题。
- 什么是设计模式
- HTTP Header 属性列表
- PHP数据库长连接mysql_pconnect的细节
- nginx中http核心模块的配置指令2
- nginx中http核心模块的配置指令3
- nginx中http核心模块的配置指令4
- nginx中http的fastcgi模块的配置指令1
- Php Installing An Expansion
- 如何在 Linux 中快速地通过 HTTP 提供文件访问服务
- 深入HTTP head的使用详解
- OBlog任意文件下载漏洞