exforce_download() -- 对CI中force_download()增强后的函数(不依赖CI)
2012-01-17 13:30
253 查看
/** * Enhanced force_download, can accept file in server * @author Wilson Zeng * @param $filename | The name that will use as the download file's name by default * @param $data | Can be: [1]. a file real path; [2]. an string that contain the file's content * @param $is_file | To specify the type of the $data * @return Byte Stream */ function exforce_download($filename = '', $data = '', $is_file = FALSE) { if ($filename == '' OR $data == '') { return FALSE; } if($is_file && !is_file($data)){ return FALSE; } // Try to determine if the filename includes a file extension. // We need it in order to set the MIME type if (FALSE === strpos($filename, '.')) { return FALSE; } // Grab the file extension $x = explode('.', $filename); $extension = end($x); // Load the mime types /*zcs=Reduce Dependence if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/mimes'.EXT)) { include(APPPATH.'config/'.ENVIRONMENT.'/mimes'.EXT); } elseif (is_file(APPPATH.'config/mimes'.EXT)) { include(APPPATH.'config/mimes'.EXT); } */ $mimes = array(); //zcs=[NOTE] Here we just assign an empty array to reduce the dependence of the CI framework // Set a default mime if we can't find it if ( ! isset($mimes[$extension])) { $mime = 'application/octet-stream'; } else { $mime = (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension]; } $size = 0; if($is_file){ $size = filesize($data); }else{ $size = strlen($data); } // Generate the server headers if (strpos($_SERVER['HTTP_USER_AGENT'], "MSIE") !== FALSE) { header('Content-Type: "'.$mime.'"'); header('Content-Disposition: attachment; filename="'.$filename.'"'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header("Content-Transfer-Encoding: binary"); header('Pragma: public'); header("Content-Length: ".$size); } else { header('Content-Type: "'.$mime.'"'); header('Content-Disposition: attachment; filename="'.$filename.'"'); header("Content-Transfer-Encoding: binary"); header('Expires: 0'); header('Pragma: no-cache'); header("Content-Length: ".$size); } if($is_file){ $h_file = fopen($data, 'r'); while(!feof($h_file)){ echo fread($h_file, 1048576);//1M } fclose($h_file); }else{ exit($data); } }
相关文章推荐
- 利用php CI force_download($filename, $data) 下载.csv 文件解决文件名乱码,文件内容乱码
- 利用php CI force_download($filename, $data) 下载.csv 文件解决文件名乱码,文件内容乱码
- 无损连接性、保持函数依赖
- 类对象成员函数的const增强
- 《Java编程技巧1001条》第374-401条:小心使用依赖日期的函数,
- 数据库复习(函数依赖)(转)
- 对函数依赖的一些认识
- IDOC的处理函数IDOC_INPUT_ORDERS的增强点的分析
- CI框架源码阅读笔记3 全局函数Common.php
- yum (download)rpm(force)
- Spring Aop 实现函数内部增强
- sp_depends 查询在sybase中依赖的函数
- CI框架验证码CAPTCHA辅助函数用法实例
- 模式分解、最小函数依赖集
- CI(Codeigniter)的Setting增强配置类实例
- 函数依赖与关系模式分解的一些技巧整理
- 数据库--ER模型、函数依赖、无损分解、关系代数
- Apache force file (mp3) download
- 查询日期范围增强函数 fn_ltDate3
- 函数依赖