Codeigniter如何上传图像扩展PNG和JPG

时间:2018-06-21 04:47:17

标签: php codeigniter file-upload

需要有关Framework CodeIgniter的帮助...现在我做了一个简单的上传功能。但是当我要上传文件“ image.PNG”或“ image.JPG”时,它将返回false ..但是我上传了图像文件“ image.png”或“ image.jpg”我却成功上传了。

我的控制器,我为此设置了

$config['allowed_types']  = 'gif|jpg|png|PNG|JPG'; 

和/application/config/mimes.php文件中。我还添加了

'png'   =>  array('image/png',  'image/x-png'), 
'PNG'   =>  array('image/PNG',  'image/x-PNG'),  
'jpg'   =>  array('image/jpeg', 'image/pjpeg'),
'JPG'   =>  array('image/jpeg', 'image/pjpeg'),

仍然无法工作..请帮助我

1 个答案:

答案 0 :(得分:0)

 $config['upload_path']    = FCPATH.'assets/college/images/logo/';
        $config['allowed_types']  = 'gif|jpg|png|jpeg';
        $config['max_size']       = 1000;
        $config['max_width']      = 500;
        $config['max_height']     = 500;
        $config['min_height']     = 300;
        $config['min_width']      = 300;
        $config['file_ext_tolower'] = TRUE;
        $config['file_name'] = uid().'_logo_'.date('YmdHis');
        $this->load->library('upload', $config);
        $this->upload->initialize($config);
        if ( ! $this->upload->do_upload('logo'))
        {
           set_alert('danger',htmlspecialchars(trim(strip_tags($this->upload->display_errors()))));
           redirect('somewhere');
        }
        else
        {
            $data = $this->upload->data();
            $logo['logo'] = $data['file_name'];
            $success = $this->institute_model->update_media($id,$logo);
            if($success != FALSE)
            {
                set_alert('success','Your Logo has been successfully Updated');
                redirect('institution/add_update');
            }
            else
            {
                set_alert('danger','Your logo is not updated due to some technical reasons.Please try again');
                redirect('institution/add_update');
            }
        }

使用上面的代码,了解它的功能并据此使用。不要忘记加载文件上传库。