思路步骤
* 定义参数
* 魔术方法
* 执行下载
* 获取设置属性函数
* ?????获取设置文件mime 类型
* ?????获取设置下载文件名
* 设置header
* 下载函数
实现代码
class DownFile{ ???// 定义参数 ???public $data; // 下载的数据或文件名 ???public $is_con=false; // 是否是下载内容 ???public $down_file_name; // 下载后的文件名 ???public $mime_type; ???????//下载时设置的文件类型 ???public $file_del=false; // 下载完成后是否删除服务器文件 ???private $file_ext=‘octet-stream‘; // 下载文件时设置的默认文件后缀(获取不到文件类型时设置) ???private $default_mime_type=‘application/‘; // 下载时设置的默认文件类型 ???// 魔术方法-- 对象参数赋值 ???public function __get($name) ???{ ???????return $this->name; ???} ???public function __set($name,$value) ???{ ???????if(!isset($this->name)) ???????{ ??????????exit("no is $name attr"); ???????} ???????$this->name = $value; ???} ???public function __isset($name) ???{ ???????return isset($this->name); ???} ???// 执行下载 ???public function output($data=null,$down_file_name=null,$is_con=null) ???{ ???????// 初始化赋值基本数据 ???????if(!empty($data)) ???????????$this->data=$data; ??????if(isset($is_con)) ??????????$this->is_con=$is_con; ???????// 如果下载的不是数据 并且不是文件 抛出异常 ???????if (!$this->is_con && !is_file($this->data)) { ???????????throw new Exception(‘file not exists:‘ . $this->data); ???????} ???????ob_end_clean(); ???????// 下载文件名 ???????$this->down_file_name($down_file_name); ???????if($this->is_con) ???????{ ???????????$this->mime_type=$this->default_mime_type.$this->file_ext; ???????????$file_size=strlen($this->data); ???????????$this->header($file_size); ???????????// 如果数据量过多建议 while 方式输出 ???????????/*$read_size=0; ???????????$buffer=1024; //设置一次读取的字节数,每读取一次,就输出数据(即返回给浏览器) ???????????while ($read_size<=$file_size) ???????????{ ???????????????echo substr($this->data,$read_size,$buffer); ???????????????$read_size+=$buffer; ???????????}*/ ???????????// 如果数据量小使用以下方式输出 ???????????echo $this->data; ???????}else if(is_file($this->data)) ???????{ ???????????$this->get_mime_type(); ???????????$this->down_file(filesize($this->data)); ???????} ???} ???// 获取设置文件mime 类型 ???private function get_mime_type() ???{ ???????if(empty($this->mime_type)) ???????{ ???????????$finfo = finfo_open(FILEINFO_MIME_TYPE); ???????????$this->mime_type=finfo_file($finfo, $this->data); ???????????if(empty($this->mime_type)) ???????????????$this->mime_type=$this->default_mime_type.$this->file_ext; ???????} ???} ???// 获取设置下载文件名 ???private function down_file_name($down_file_name) ???{ ???????if(!empty($down_file_name)) ???????????$this->down_file_name=$down_file_name; ???????else ???????{ ???????????if(empty($this->down_file_name)) ???????????????$this->down_file_name=time().$this->file_ext; ???????} ???} ???// 设置header ???private function header($f_size) ???{ ???????header("Content-type:".$this->mime_type); ???????header("Accept-Ranges:bytes"); ???????header("Accept-Length:".$f_size); ???????header("Content-Disposition:attachment;filename=".$this->down_file_name); ???????header("Content-Transfer-Encoding:binary"); ???????header("Cache-Control:no-cache,no-store,max-age=0,must-revalidate"); ???????header("Pragma:no-cache"); ???} ???// 下载文件函数 ???private function down_file($f_size) ???{ ???????$this->header($f_size); ???????if(!$this->file_del) ???????{ ???????????// 如果文件过大建议使用 while 方式读取输出 ???????????/*$fp=fopen($this->data,"r"); ???????????$buffer=1024; //设置一次读取的字节数,每读取一次,就输出数据(即返回给浏览器) ???????????while(!feof($fp)) ???????????{ ???????????????$file_con=fread($fp,$buffer); // fread 指针自动下移 ???????????????echo $file_con; ???????????} ???????????fclose($fp);*/ ???????????????????????// 如果数据量小直接输出 ???????????readfile($this->data); ???????}else ???????{ ???????????$fp=fopen($this->data,"r"); ???????????$buffer=1024; //设置一次读取的字节数,每读取一次,就输出数据(即返回给浏览器) ???????????$file_count=0; //读取的总字节数 ???????????//向浏览器返回数据 ???????????while(!feof($fp) && $file_count<$f_size){ ???????????????$file_con=fread($fp,$buffer); ???????????????$file_count+=$buffer; ???????????????echo $file_con; ???????????} ???????????fclose($fp); ???????????//下载完成后删除压缩包,临时文件夹 ???????????if($file_count >= $f_size) ???????????{ ???????????????@unlink($this->data); ???????????} ???????} ???}}
调用示例
$f=new DownFile();//$f->output(‘1.png‘,‘2.png‘); // 参数调用-- 下载文件//$f->output(‘测试数据下载直接生成文件‘,‘2.txt‘,true); // 参数调用-- 下载数据直接生成文件// 对象方式调用/*$f->data=‘1.html‘;$f->down_file_name=‘2.html‘;//$f->file_del=true; ?// 下载完成后删除服务器远程文件$f->output();*/$f->data=‘对象方式调用‘.PHP_EOL.‘测试数据下载直接生成文件‘;$f->down_file_name=‘2.txt‘;$f->is_con=true;$f->output();
php 下载文件/直接下载数据内容
原文地址:https://www.cnblogs.com/xuey/p/10353276.html