用户
 找回密码
 入住 CI 中国社区
搜索
查看: 1751|回复: 4
收起左侧

[版本 2.x] 文件上传类的allowed_types很怎么设置?

[复制链接]
发表于 2015-3-24 10:18:55 | 显示全部楼层 |阅读模式
手册上写的allowed_types是允许上传文件的MIME类型;


测试几个常用的jpg png zip 都没有问题


但是有的类型就不行 例如:rar  doc docx


MIME类型应该不能直接填文件后缀名的啊。
 楼主| 发表于 2015-3-24 10:29:43 | 显示全部楼层
Upload.php的源码中有下面这个函数。奇怪的是为什么这个类没有在任何地方调用呢?

那传进来的allowed_types是在哪里设置的呢?

看不懂。求大神指点。

PHP复制代码
        public function set_allowed_types($types)
        {
                if ( ! is_array($types) && $types == '*')
                {
                        $this->allowed_types = '*';
                        return;
                }
                $this->allowed_types = explode('|', $types);
        }
 
 
复制代码


 楼主| 发表于 2015-3-24 10:36:06 | 显示全部楼层
是在initialize中调用的
PHP复制代码
                foreach ($defaults as $key => $val)
                {
                        if (isset($config[$key]))
                        {
                                $method = 'set_'.$key;
                                if (method_exists($this, $method))
                                {
                                        $this->$method($config[$key]);
                                }
                                else
                                {
                                        $this->$key = $config[$key];
                                }
                        }
                        else
                        {
                                $this->$key = $val;
                        }
                }
 
 
复制代码


 楼主| 发表于 2015-3-24 10:39:49 | 显示全部楼层
PHP复制代码
public function is_allowed_filetype($ignore_mime = FALSE)
        {
                if ($this->allowed_types == '*')
                {
                        return TRUE;
                }
 
                if (count($this->allowed_types) == 0 OR ! is_array($this->allowed_types))
                {
                        $this->set_error('upload_no_file_types');
                        return FALSE;
                }
 
                $ext = strtolower(ltrim($this->file_ext, '.'));
 
                if ( ! in_array($ext, $this->allowed_types))
                {
                        return FALSE;
                }
 
                // Images get some additional checks
                $image_types = array('gif', 'jpg', 'jpeg', 'png', 'jpe');
 
                if (in_array($ext, $image_types))
                {
                        if (getimagesize($this->file_temp) === FALSE)
                        {
                                return FALSE;
                        }
                }
 
                if ($ignore_mime === TRUE)
                {
                        return TRUE;
                }
 
                $mime = $this->mimes_types($ext);
 
                if (is_array($mime))
                {
                        if (in_array($this->file_type, $mime, TRUE))
                        {
                                return TRUE;
                        }
                }
                elseif ($mime == $this->file_type)
                {
                                return TRUE;
                }
 
                return FALSE;
        }
 
 
复制代码



这个函数看不懂了。为什么还要有$image_types下面的判断?
 楼主| 发表于 2015-3-24 10:55:09 | 显示全部楼层
我觉得应该是下面这段代码不够完善:
PHP复制代码
 
                $mime = $this->mimes_types($ext);
 
                if (is_array($mime))
                {
                        if (in_array($this->file_type, $mime, TRUE))
                        {
                                return TRUE;
                        }
                }
                elseif ($mime == $this->file_type)
                {
                                return TRUE;
                }
 
 
复制代码


mimes.php配置文件中配置了 后缀名与mime的对应关系。

但是有的后缀名是没有的。例如rar 。

本版积分规则