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

[上传/下载] pptx的格式无法上传,

[复制链接]
发表于 2017-7-26 12:12:32 | 显示全部楼层 |阅读模式
$config ['allowed_types'] = 'xls|xlsx|doc|docx|ppt|pptx|xl|gif|jpg|png|txt';
                $config ['max_size'] = '20000';
                $config ['file_name'] = date ( 'Ymdhis', time () );
                $this->load->library ( 'upload', $config );
                $file_name='';

                if (! $this->upload->do_upload ("file_import")) {
//                        $error = array ('error' => $this->upload->display_errors () );
//                        echo $error ['error'];
//                        exit ();
//                        echo $_FILES["file_import"]["name"];die();
                } else {

                        $data = array ('upload_data' => $this->upload->data () );
                        $file_name = $data ['upload_data'] ['file_name'];

                }
上传的pptx文件时,$this->upload->do_upload(‘file_import’)为空,ppt格式可以
发表于 2017-7-26 12:26:40 | 显示全部楼层
有可能是 mime 类型不对
发表于 2017-8-18 17:45:25 | 显示全部楼层
Hex 发表于 2017-7-26 12:26
有可能是 mime 类型不对

已找到方法解决,可以成功上传pptx,由于是菜鸟,即使解决了问题还是不知道原理是什么?
在CI框架中system/libraries/Upload类中,修改is_allowed_filetype方法中的以下代码,就可以上传pptx格式。
public function is_allowed_filetype($ignore_mime = FALSE)
        {

                if ($this->allowed_types === '*')
                {
                        return TRUE;
                }

                if (empty($this->allowed_types) OR ! is_array($this->allowed_types))
                {

                        $this->set_error('upload_no_file_types', 'debug');
                        return FALSE;
                }

                $ext = strtolower(ltrim($this->file_ext, '.'));

                if ( ! in_array($ext, $this->allowed_types, TRUE))
                {
                        return FALSE;
                }

                // Images get some additional checks
                if (in_array($ext, array('gif', 'jpg', 'jpeg', 'jpe', 'png'), TRUE) && @getimagesize($this->file_temp) === FALSE)
                {
                        return FALSE;
                }

                if ($ignore_mime === TRUE)
                {
                        return TRUE;
                }

                if (isset($this->_mimes[$ext]))
                {
//                        return is_array($this->_mimes[$ext])
//                                ? in_array($this->file_type, $this->_mimes[$ext], TRUE)
//                                : ($this->_mimes[$ext] === $this->file_type);
                        //修改以下部分,便可允许上传pptx,上面是源码!
                        return is_array($this->_mimes[$ext])
                                ? in_array($this->file_type, $this->_mimes[$ext], TRUE)||in_array($this->file_type, array('application/vnd.ms-powerpoint'), TRUE)
                                : ($this->_mimes[$ext] === $this->file_type);
                }

                return FALSE;
        }
发表于 2017-8-21 10:26:21 | 显示全部楼层
CCchen123 发表于 2017-8-18 17:45
已找到方法解决,可以成功上传pptx,由于是菜鸟,即使解决了问题还是不知道原理是什么?
在CI框架中system ...

把 application/vnd.ms-powerpoint 这个加到 config/mimes.php 里试试,这里的原因主要是 CI 不只是验证扩展名,还必须要验证 mimes 类型。
发表于 2017-8-22 15:14:58 | 显示全部楼层
Hex 发表于 2017-8-21 10:26
把 application/vnd.ms-powerpoint 这个加到 config/mimes.php 里试试,这里的原因主要是 CI 不只是验证 ...

谢谢,早上在群里看到了,已经加了,可以了,谢谢!

本版积分规则