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

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

代码如下:

  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * Force Download
  4. *
  5. * Generates headers that force a download to happen
  6. *
  7. * @access public
  8. * @param string filename
  9. * @param mixed the data to be downloaded
  10. * @return void
  11. */
  12. if ( ! function_exists('force_download'))
  13. {
  14. function force_download($filename = '', $file = '')
  15. {
  16. if ($filename == '' OR $file == '')
  17. {
  18. return FALSE;
  19. }
  20.  
  21. // Try to determine if the filename includes a file extension.
  22. // We need it in order to set the MIME type
  23. if (FALSE === strpos($filename, '.'))
  24. {
  25. return FALSE;
  26. }
  27.  
  28. // Grab the file extension
  29. $x = explode('.', $filename);
  30. $extension = end($x);
  31.  
  32. // Load the mime types
  33. if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'))
  34. {
  35. include(APPPATH.'config/'.ENVIRONMENT.'/mimes.php');
  36. }
  37. elseif (is_file(APPPATH.'config/mimes.php'))
  38. {
  39. include(APPPATH.'config/mimes.php');
  40. }
  41.  
  42. // Set a default mime if we can't find it
  43. if ( ! isset($mimes[$extension]))
  44. {
  45. $mime = 'application/octet-stream';
  46. }
  47. else
  48. {
  49. $mime = (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];
  50. }
  51.  
  52. // Generate the server headers
  53. if (strpos($_SERVER['HTTP_USER_AGENT'], "MSIE") !== FALSE)
  54. {
  55. header('Content-Type: "'.$mime.'"');
  56. header('Content-Disposition: attachment; filename="'.$filename.'"');
  57. header('Expires: 0');
  58. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  59. header("Content-Transfer-Encoding: binary");
  60. header('Pragma: public');
  61. header("Content-Length: ".filesize($file));
  62. }
  63. else
  64. {
  65. header('Content-Type: "'.$mime.'"');
  66. header('Content-Disposition: attachment; filename="'.$filename.'"');
  67. header("Content-Transfer-Encoding: binary");
  68. header('Expires: 0');
  69. header('Pragma: no-cache');
  70. header("Content-Length: ".filesize($file));
  71. }
  72.  
  73. readfile_chunked($file);
  74. die;
  75. }
  76. }
  77.  
  78. /**
  79. * readfile_chunked
  80. *
  81. * Reads file in chunks so big downloads are possible without changing PHP.INI
  82. *
  83. * @access public
  84. * @param string file
  85. * @param boolean return bytes of file
  86. * @return void
  87. */
  88. if ( ! function_exists('readfile_chunked'))
  89. {
  90. function readfile_chunked($file, $retbytes=TRUE)
  91. {
  92. $chunksize = 1 * (1024 * 1024);
  93. $buffer = '';
  94. $cnt =0;
  95.  
  96. $handle = fopen($file, 'r');
  97. if ($handle === FALSE)
  98. {
  99. return FALSE;
  100. }
  101.  
  102. while (!feof($handle))
  103. {
  104. $buffer = fread($handle, $chunksize);
  105. echo $buffer;
  106. ob_flush();
  107. flush();
  108.  
  109. if ($retbytes)
  110. {
  111. $cnt += strlen($buffer);
  112. }
  113. }
  114.  
  115. $status = fclose($handle);
  116.  
  117. if ($retbytes AND $status)
  118. {
  119. return $cnt;
  120. }
  121.  
  122. return $status;
  123. }
  124. }
  125.  
  126. /* End of file MY_download_helper.php */
  127. /* Location: ./application/helpers/MY_download_helper.php */

小提示:

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

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

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

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

    

codeigniter文件上传问题的更多相关文章

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

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

  2. CodeIgniter学习笔记五:分页,文件上传,session,验证码

    一.分页 示例代码: //装载类文件 $this -> load -> library('pagination'); $controller = $this->router-> ...

  3. jquery.uploadify文件上传组件

    1.jquery.uploadify简介 在ASP.NET中上传的控件有很多,比如.NET自带的FileUpload,以及SWFUpload,Uploadify等等,尤其后面两个控件的用户体验比较好, ...

  4. 11、Struts2 的文件上传和下载

    文件上传 表单准备 要想使用 HTML 表单上传一个或多个文件 须把 HTML 表单的 enctype 属性设置为 multipart/form-data 须把 HTML 表单的method 属性设置 ...

  5. Java FtpClient 实现文件上传服务

    一.Ubuntu 安装 Vsftpd 服务 1.安装 sudo apt-get install vsftpd 2.添加用户(uftp) sudo useradd -d /home/uftp -s /b ...

  6. 小兔Java教程 - 三分钟学会Java文件上传

    今天群里正好有人问起了Java文件上传的事情,本来这是Java里面的知识点,而我目前最主要的精力还是放在了JS的部分.不过反正也不麻烦,我就专门开一贴来聊聊Java文件上传的基本实现方法吧. 话不多说 ...

  7. ,net core mvc 文件上传

    工作用到文件上传的功能,在这个分享下 ~~ Controller: public class PictureController : Controller { private IHostingEnvi ...

  8. Web开发安全之文件上传安全

    很长一段时间像我这种菜鸡搞一个网站第一时间反应就是找上传,找上传.借此机会把文件上传的安全问题总结一下. 首先看一下DVWA给出的Impossible级别的完整代码: <?php if( iss ...

  9. AutoIt实现Webdriver自动化测试文件上传

    在运用WebDriver进行自动化测试时,由于WebDriver自身的限制,对于上传文件时Windows弹出的文件选择窗口无法控制,通过在网上查找资料锁定使用AutoIt来控制文件上传窗口. Auto ...

随机推荐

  1. IP欺骗原理与过程分析

    IP欺骗攻击法 原创:r00t <r00t@unsecret.org> QQ: 22664566 http://www.unsecret.org --------------------- ...

  2. Python之路【第三篇】:Python基础(二)

    函数的理解 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 函数作用是你的程序有良好的扩展性.复用性. 同样的功能要是用3次以上的话就建议 ...

  3. js中自定义事件,使用了jQuery

    $(function(){ $('#btn').bind("myClick", function(){ //自定义myClick事件 $('#test').append(" ...

  4. [Angularjs]系列——学习与实践

    写在前面 这个系列是来这家公司到现在,边用边学,以及在工作中遇到的问题进行的总结.深入的东西不多,大多是实际开发中用到的东西. 这里做一个目录,方便查看. 系列文章 [Angularjs]ng-sel ...

  5. GOF业务场景的设计模式-----责任链模式

    定义:使多个对象都有机会处理请求,从而避免了请求的发送者和接收者之间的耦合关系.将这些对象连成一条链,并沿着这条链传递该请求,直到有对象处理它为止. 首先来看一段代码: public void tes ...

  6. 前端入门级之如何从零开始前端(估计要被人鄙视成LOW货了)入门篇

    <!------------------------------------------------------基本说明开始----------------------------------- ...

  7. thinkPHP3.2.3集成swoole扩展

    swoole.php #!/bin/env php <?php /** * 默认时区定义 */ date_default_timezone_set('Asia/Shanghai'); /** * ...

  8. Roslyn词法分析器初使用

    需:install-package:Microsoft.CodeAnalysis ];         NamespaceDeclarationSyntax NameSpaceDeclaration  ...

  9. jquery隐藏按钮

    $(function () { jhbs = getQueryStringByName('jhbs'); shhbs = getQueryStringByName('shhbs'); if (shhb ...

  10. 【GXZ的原创】C++小游戏——五子棋

    前些时候考完试自己编的带有胜负判定的五子棋. 操作方法:WSAD或↑↓←→移动下棋位置,Space或Enter放置. 如果游戏出现bug,欢迎大家在评论区反馈. #include <stdio. ...