Codeigniter的文件上传类方便了我们使用PHP来处理文件上传的操作,使用起来非常简单,如下:
 
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width']  = '1024';
$config['max_height']  = '768';
 
$this->load->library('upload', $config);
 
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
 
如果只是处理图片类型的文件,基本上不会遇到这个坑,如果处理到了 excel、zip、rar类型的文件,你可能就会遇到明明在 allowed_types 中允许的文件类型,最后收获了 “The filetype you are attempting to upload is not allowed.”的错误,为什么会这样呢?
 
Codeigniter的文件上传类型判断在 is_allowed_filetype 这个函数中处理,造成这个错误的主要原因是因为判断逻辑中有一个 mime 类型判断的步骤。
 
什么是 Mime 呢?
 
MIME是Multipurpose Internet Mail Extention的缩写,是描述消息内容类型的互联网标准。
 
为什么需要判断 Mime?因为如果只从文件后缀来判断文件类型,是非常危险的。不怀好意的用户可能会把一个可执行文件后缀改成图片类型,上传成功后,如果能够获得文件的地址,并且文件在可执行目录,就能够执行动态脚本,还是很危险的。著名的DedeCMS就很多这种漏洞。
 
针对不同的后缀,Codeigniter会从 config/mimes.php 文件匹配POST过来的数据中的 file_type 属性,只有一样才会校验通过,否则就会发生文件类型不匹配的错误。
 
找到问题的原因,解决起来就很方便了。我们只需要在 config/mimes.php 文件中,添加对应的后缀以及file_type 这样就能解决这个问题。下面是我为几种常见文件增加的配置:
 
'docx'     =>     array('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/zip'),
'xlsx'     =>     array('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/zip'),
'xlsm'     =>     array('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet','application/vnd.ms-excel.sheet.macroenabled.12', 'application/zip'),
'word'     =>     array('application/msword', 'application/octet-stream'),
'rar'     =>     array('application/octet-stream'),
'zip'     =>  array('application/x-zip', 'application/zip', 'application/x-zip-compressed', 'application/octet-stream'),
 
补充:我这里使用的Codeigniter是2.x版本的,至于现在3.x版本中是否还存在这个问题并没有测试,有遇到的朋友可以分享一下。
 
参考资料:

Codeigniter文件上传类型不匹配错误的更多相关文章

  1. PHP图片文件上传类型限制扩展名限制大小限制与自动检测目录创建。

    程序测试网址:http://blog.z88j.com/fileuploadexample/index.html 代码分为两部分: 一部分form表单: <!doctype html> & ...

  2. jsp页面判断文件上传类型

    <script language="javascript" type="text/javascript"> function check_file( ...

  3. input file实现多选和限制文件上传类型

    <!-- input file  accept 属性设置可上传文件的类型  multiple属性设置可多文件上传--> <!-- accept 并未真正的实现限制上传文件类型,只是在 ...

  4. uploadify 文件上传报http 302错误

    uploadify文件上传会报http 302错误,在配置文件中将处理上传的通用类取消验证, 假设上传的通用处理类是fileUpload.ashx,则在配置文件同添加下面过滤配置能解决问题. < ...

  5. input file实现多选,限制文件上传类型,图片上传前预览功能

    限制上传类型 & 多选:① accept 属性只能与 <input type="file" /> 配合使用.它规定能够通过文件上传进行提交的文件类型. ② mu ...

  6. JavaScript 文件上传类型判断

    文件上传时用到一个功能,使用html元素的input标签实现, <input id="imageFile" name="imageFile1" accep ...

  7. struts2文件上传类型的过滤

    转自:http://www.2cto.com/kf/201403/282787.html 第一种解决方案: 1.手动实现文件过滤: 判断上传的文件是否在允许的范围内定义该Action允许上传的文件类型 ...

  8. input的文件上传类型判断

    参考网址: http://www.helloweba.com/view-blog-224.html <p> <label>请选择一个图像文件:</label> &l ...

  9. codeigniter文件上传问题

    codeigniter自带的文件下载辅助函数非常简单实用,但是在处理大文件的时候,就显得捉襟见肘. 在网上找到了一个对download_helper.php文件的扩展,非常好用,记录下,遇到相同问题的 ...

随机推荐

  1. 用linq批量更新数据集

    对于数据集需要更新所有对象的FTaxRate 赋值为ftax_rate 以下采用遍历方式更新: foreach (var entry in _dataEntityList){ entry.FTaxRa ...

  2. gitlab改用ssh操作

    1.配置ssh http://blog.csdn.net/xyzchenxiaolin/article/details/51853319http://blog.csdn.net/r8hzgemq/ar ...

  3. Java提高篇——Java 异常处理

    异常的概念 异常是程序中的一些错误,但并不是所有的错误都是异常,并且错误有时候是可以避免的. 比如说,你的代码少了一个分号,那么运行出来结果是提示是错误java.lang.Error:如果你用Syst ...

  4. jq弹框确认

    function delCustomer(id,num){ var r=confirm("友情提醒:确认要删除客户吗?"); if (r==true){ $.ajax({ type ...

  5. leetcode bugfree note

    463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...

  6. U家面试prepare: Serialize and Deserialize Tree With Uncertain Children Nodes

    Like Leetcode 297, Serialize and Deserialize Binary Tree, the only difference, this is not a binary ...

  7. android adb命令

    adb查看logcat adb logcat <TAG>:* *:S 查看指定TAG的Log adb查看最上层activity:linux: adb shell dumpsys activ ...

  8. 有关css伪类visited样式无效的解决方法

    错误写法 将visited写在hover和active之后,例如: .ui-page-theme-a .digilinx-ui-btn{background:#00a325;border-color: ...

  9. Hadoop:操作 Hadoop Cluster

    启动Hadoop 当完成所有的必要配置后,将HADOOP_CONF_DIR目录中的所有配置文件复制到所有机器,建议将HDFS和YARN后台进程一不同的用户身份运行,比如运行HDFS进程们的用户为hdf ...

  10. 性能调优利器之strace

    最近需要对一个自己开发的socket server的性能进行分析,刚开始还想了好长时间怎么来分析.后来才意识到其实使用strace就足够了. 观察到的现象是server单进程CPU使用率97,但磁盘i ...