codeigniter自带的文件下载辅助函数非常简单实用,但是在处理大文件的时候,就显得捉襟见肘。

在网上找到了一个对download_helper.php文件的扩展,非常好用,记录下,遇到相同问题的猿友们可以借鉴下。

代码如下:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*** Force Download** Generates headers that force a download to happen** @access    public* @param    string    filename* @param    mixed    the data to be downloaded* @return    void*/
if ( ! function_exists('force_download'))
{function force_download($filename = '', $file = ''){if ($filename == '' OR $file == ''){return FALSE;}// Try to determine if the filename includes a file extension.// We need it in order to set the MIME typeif (FALSE === strpos($filename, '.')){return FALSE;}// Grab the file extension$x = explode('.', $filename);$extension = end($x);// Load the mime typesif (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/mimes.php')){include(APPPATH.'config/'.ENVIRONMENT.'/mimes.php');}elseif (is_file(APPPATH.'config/mimes.php')){include(APPPATH.'config/mimes.php');}// Set a default mime if we can't find itif ( ! isset($mimes[$extension])){$mime = 'application/octet-stream';}else{$mime = (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];}// Generate the server headersif (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: ".filesize($file));}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: ".filesize($file));}readfile_chunked($file);die;}
}/*** readfile_chunked** Reads file in chunks so big downloads are possible without changing PHP.INI** @access    public* @param    string    file* @param    boolean    return bytes of file* @return    void*/
if ( ! function_exists('readfile_chunked'))
{function readfile_chunked($file, $retbytes=TRUE){$chunksize = 1 * (1024 * 1024);$buffer = '';$cnt =0;$handle = fopen($file, 'r');if ($handle === FALSE){return FALSE;}while (!feof($handle)){$buffer = fread($handle, $chunksize);echo $buffer;ob_flush();flush();if ($retbytes){$cnt += strlen($buffer);}}$status = fclose($handle);if ($retbytes AND $status){return $cnt;}return $status;}
}/* End of file MY_download_helper.php */
/* Location: ./application/helpers/MY_download_helper.php */

小提示:

  @ 使用的时候,别忘了先加载

    $this->load->helper('download');

  @ 该扩展和原生的force_download($filename = '', $data = '')函数的第二个参数有所不同

    原生的$data为一个字符串,而该函数的$file为需要下载的文件的物理路径!大概是因为fread()只能正确读取全路径的文件的缘故吧,没有求证,知道的猿友请帮忙解释下,谢谢!

    

转载于:https://www.cnblogs.com/shaoyikai/p/3837490.html

codeigniter文件上传问题相关推荐

  1. Codeigniter文件上传类型不匹配错误

    Codeigniter的文件上传类方便了我们使用PHP来处理文件上传的操作,使用起来非常简单,如下: $config['upload_path'] = './uploads/'; $config['a ...

  2. 雷林鹏分享:codeigniter框架文件上传处理

    CodeIgniter 框架input表单的重新填充,主要是针对text.radio.checkbox.select等input表单,那么对于文件上传表单file该如何处理呢? 自己的处理方式: // ...

  3. php vsftpd文件上传类,php ftp文件上传函数(基础版)

    php ftp文件上传函数(基础版) 复制代码 代码如下: // 定义变量 $local_file = 'local.zip'; $server_file = 'server.zip'; // 连接F ...

  4. jquery uploadify 多文件上传插件 使用经验

    2019独角兽企业重金招聘Python工程师标准>>> Uploadify 官网:http://www.uploadify.com/ 一.如何使用呢? 官网原文:http://www ...

  5. html5支持ajax和jQuery吗,使用HTML5文件上传与AJAX和jQuery(Using HTML5 file uploads with AJAX and jQuery)...

    使用HTML5文件上传与AJAX和jQuery(Using HTML5 file uploads with AJAX and jQuery) 诚然,Stack Overflow上有类似的问题,但似乎并 ...

  6. php文件上传实验总结,PHP文件上传一些小收获

    又码了一个周末的代码,这次在做一些关于文件上传的东西.(PHP UPLOAD)小有收获项目是一个BT种子列表,用户有权限上传自己的种子,然后配合BT TRACK服务器把种子的信息写出来. 开始觉得这玩 ...

  7. springboot设置文件上传大小(tomcat默认1M)

    application.yml # 设置文件上传大小(tomcat默认1M) server:tomcat:max-http-form-post-size: -1 spring:servlet:mult ...

  8. 将文件上传至ftp服务器,FTP文件上传工具类,将文件上传至服务器指定目录

    将文件上传至ftp服务器,传入File对象,将文件上传至ftp服务器 需要配置修改的点: 1. 服务器ip端口(服务器ip 端口22/21). 2. 服务器账号密码(服务器登录用户名密码). 3. 上 ...

  9. Springboot 多文件上传

    其实多个文件和单个文件上传是一样的,可以使用同一个Controller 添加依赖 <!-- https://mvnrepository.com/artifact/commons-fileuplo ...

最新文章

  1. mysql 执行计划 什么用_简述MySQL的执行计划的作用及使用方法
  2. 【算法】并查集刷题总结
  3. 2021年第十六届全国大学生智能汽车竞赛线上比赛的广东、山东赛区赛道铺设规范
  4. 信息北航身份认证_信息北航丨北航第一服务平台,你值得关注!
  5. 蓝桥备赛第一周2021.1.11 递归 枚举 位运算
  6. 【渝粤题库】国家开放大学2021春2511幼儿园课程论题目
  7. const型数据小结
  8. 使用 Spring 简化 MyBatis
  9. Linux环境下rman备份的实例脚本
  10. 如何在WP7中使用自定义字体
  11. 计算机与临床医学的应用,论临床医学教育中计算机系统的应用.pdf
  12. paip.版本控件svn删除文件或目录后的恢复
  13. Linux关闭桌面进程,Centos进入桌面和退出桌面的方法
  14. Hello Qt(十六)——QT绘图实例-钟表
  15. 线段与线段交点的求解
  16. OVP 过压保护电路
  17. MPU6050介绍及姿态解算
  18. Automation Anywhere 认证考试
  19. clion安装与配置(一 - WSL)
  20. 小Q书桌的下载、安装和使用

热门文章

  1. 【linux家常菜】redhat 6.5 安装yum
  2. Java I/O模型从BIO到NIO和Reactor模式
  3. zcmu-2149(归并排序)
  4. 初识Frida--Android逆向之Java层hook (二)
  5. 大并发服务器架构 大型网站架构演变
  6. 区块链基础知识系列 第四课Hyperledger fabric 1.0网络组成及构建流程
  7. NOIP2018比赛总结
  8. 加载svr模型_机器学习XGBoost实战,网格搜索自动调参,对比随机森林,线性回归,SVR【完整代码(含注释)+数据集见原文链接】...
  9. php代码文本型怎么更改成数值型,php 怎么将字符转成数字
  10. 怎么安装python3.6.2_centos7.2下安装python3.6.2