codeigniter文件上传问题
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文件上传问题的更多相关文章
- Codeigniter文件上传类型不匹配错误
Codeigniter的文件上传类方便了我们使用PHP来处理文件上传的操作,使用起来非常简单,如下: $config['upload_path'] = './uploads/'; $config[ ...
- CodeIgniter学习笔记五:分页,文件上传,session,验证码
一.分页 示例代码: //装载类文件 $this -> load -> library('pagination'); $controller = $this->router-> ...
- jquery.uploadify文件上传组件
1.jquery.uploadify简介 在ASP.NET中上传的控件有很多,比如.NET自带的FileUpload,以及SWFUpload,Uploadify等等,尤其后面两个控件的用户体验比较好, ...
- 11、Struts2 的文件上传和下载
文件上传 表单准备 要想使用 HTML 表单上传一个或多个文件 须把 HTML 表单的 enctype 属性设置为 multipart/form-data 须把 HTML 表单的method 属性设置 ...
- Java FtpClient 实现文件上传服务
一.Ubuntu 安装 Vsftpd 服务 1.安装 sudo apt-get install vsftpd 2.添加用户(uftp) sudo useradd -d /home/uftp -s /b ...
- 小兔Java教程 - 三分钟学会Java文件上传
今天群里正好有人问起了Java文件上传的事情,本来这是Java里面的知识点,而我目前最主要的精力还是放在了JS的部分.不过反正也不麻烦,我就专门开一贴来聊聊Java文件上传的基本实现方法吧. 话不多说 ...
- ,net core mvc 文件上传
工作用到文件上传的功能,在这个分享下 ~~ Controller: public class PictureController : Controller { private IHostingEnvi ...
- Web开发安全之文件上传安全
很长一段时间像我这种菜鸡搞一个网站第一时间反应就是找上传,找上传.借此机会把文件上传的安全问题总结一下. 首先看一下DVWA给出的Impossible级别的完整代码: <?php if( iss ...
- AutoIt实现Webdriver自动化测试文件上传
在运用WebDriver进行自动化测试时,由于WebDriver自身的限制,对于上传文件时Windows弹出的文件选择窗口无法控制,通过在网上查找资料锁定使用AutoIt来控制文件上传窗口. Auto ...
随机推荐
- IP欺骗原理与过程分析
IP欺骗攻击法 原创:r00t <r00t@unsecret.org> QQ: 22664566 http://www.unsecret.org --------------------- ...
- Python之路【第三篇】:Python基础(二)
函数的理解 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 函数作用是你的程序有良好的扩展性.复用性. 同样的功能要是用3次以上的话就建议 ...
- js中自定义事件,使用了jQuery
$(function(){ $('#btn').bind("myClick", function(){ //自定义myClick事件 $('#test').append(" ...
- [Angularjs]系列——学习与实践
写在前面 这个系列是来这家公司到现在,边用边学,以及在工作中遇到的问题进行的总结.深入的东西不多,大多是实际开发中用到的东西. 这里做一个目录,方便查看. 系列文章 [Angularjs]ng-sel ...
- GOF业务场景的设计模式-----责任链模式
定义:使多个对象都有机会处理请求,从而避免了请求的发送者和接收者之间的耦合关系.将这些对象连成一条链,并沿着这条链传递该请求,直到有对象处理它为止. 首先来看一段代码: public void tes ...
- 前端入门级之如何从零开始前端(估计要被人鄙视成LOW货了)入门篇
<!------------------------------------------------------基本说明开始----------------------------------- ...
- thinkPHP3.2.3集成swoole扩展
swoole.php #!/bin/env php <?php /** * 默认时区定义 */ date_default_timezone_set('Asia/Shanghai'); /** * ...
- Roslyn词法分析器初使用
需:install-package:Microsoft.CodeAnalysis ]; NamespaceDeclarationSyntax NameSpaceDeclaration ...
- jquery隐藏按钮
$(function () { jhbs = getQueryStringByName('jhbs'); shhbs = getQueryStringByName('shhbs'); if (shhb ...
- 【GXZ的原创】C++小游戏——五子棋
前些时候考完试自己编的带有胜负判定的五子棋. 操作方法:WSAD或↑↓←→移动下棋位置,Space或Enter放置. 如果游戏出现bug,欢迎大家在评论区反馈. #include <stdio. ...