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 type
if (FALSE === strpos($filename, '.'))
{
return FALSE;
} // Grab the file extension
$x = explode('.', $filename);
$extension = end($x); // Load the mime types
if (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 it
if ( ! isset($mimes[$extension]))
{
$mime = 'application/octet-stream';
}
else
{
$mime = (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];
} // 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: ".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()只能正确读取全路径的文件的缘故吧,没有求证,知道的猿友请帮忙解释下,谢谢!

    

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. HTTP Status Code [RFC]

    来源:http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml Hypertext Transfer Prot ...

  2. C++strng流(入门级)

    /************************************************************************* * * FILENAME: stringTest. ...

  3. $().index() 两种用法

    第一种:获得第一个 p 元素的名称和值: $(this).index() <script type="text/javascript"> $(document).rea ...

  4. jsp机制基础

    JSP 和Servlet技术一样,JSP也是SUN公司定义的一种开发动态web资源的技术,属于JavaEE技术之一.JSP实际上就是Servlet,它们在一起又称JSP/Servlet规范. Serv ...

  5. 求1到n的阶乘

    #include<stdio.h> int main() { int data; ; scanf("%d",&data); ){ int j; ;j<=d ...

  6. firefox 提示 setTimeout():useless setTimeout call (missing quotes around argument?) 错误

    原来代码: setTimeout(window.parent.refreshNode(id),500);// 500毫秒后,调用父窗口的refreshNode()方法 refreshNode()方法总 ...

  7. 深入理解Java:注解(Annotation)--注解处理器

    如果没有用来读取注解的方法和工作,那么注解也就不会比注释更有用处了.使用注解的过程中,很重要的一部分就是创建于使用注解处理器.Java SE5扩展了反射机制的API,以帮助程序员快速的构造自定义注解处 ...

  8. 【转】随机函数 rand() srand() 以及seed的原理

    from:http://blog.csdn.net/feige2008/article/details/6943885   标准库<cstdlib>(被包含于<iostream> ...

  9. 给各位聚聚和大大介绍一个开源项目 Expression2Sql(转)

    阅读目录 一.Expression2Sql介绍 二.单表简单查询 三.Where条件 四.多表关联查询 五.group by 六.order by 七.函数 八.delete 删除 九.update ...

  10. [慢查优化]联表查询注意谁是驱动表 & 你搞不清楚谁join谁更好时请放手让mysql自行判定

    写在前面的话: 不要求每个人一定理解 联表查询(join/left join/inner join等)时的mysql运算过程: 不要求每个人一定知道线上(现在或未来)哪张表数据量大,哪张表数据量小: ...