无法插入图片上传的数据


Can not insert the data with image upload

当运行成功时,我进行了插入数据和上传图像到数据库中,但是当插入数据和上传图像包含错误时。

图片路径不正确。

您的服务器不支持处理这种类型的图像所需的GD功能。

如何处理如果我插入数据而不上传图像是不错误的?图像数据库进入默认图像?

This my controllers

public function save(){   
    $this->load->library('image_lib');
    //$nama_asli = $_FILES['userfile']['name'];
    $id = $this->input->post('id',TRUE);
    $config['file_name'] = $id ;//'_'.'_'.$nama_asli;
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png|jpeg|bmp';
    $config['max_size'] = '100000';
    
    $this->load->library('upload', $config);
    
    $this->upload->initialize($config);
    if ( ! $this->upload->do_upload('userfile'))
    {
        $files = $this->upload->data();
        $fileNameResize = $config['upload_path'].$config['file_name'];
        $size =  array(             
                    array('name'    => 'thumb','width'  => 100, 'height'    => 100, 'quality'   => '100%')
                );
        $resize = array();
        foreach($size as $r){               
            $resize = array(
                "width"         => $r['width'],
                "height"        => $r['height'],
                "quality"       => $r['quality'],
                "source_image"  => $fileNameResize,
                "new_image"     => $url.$r['name'].'/'.$config['file_name']
            );
        $this->image_lib->initialize($resize); 
            if(!$this->image_lib->resize())                 
            die($this->image_lib->display_errors());
        }   
        
    }
    else
    {
        $data = array('upload_data' => $this->upload->data());      
        $get_name = $this->upload->data();
        $nama_foto = $get_name['file_name'];
        $this->mcrud->savealat($nama_foto);
        redirect('instrument/detailalat');      
    }
}   

这是我的模型

function savealat($nama_foto) {
    $data = array(
        'id' => $this->input->post('id'),
        'namaalat' => $this->input->post('namaalat'),
        'dayalistrik' => $this->input->post('dayalistrik'),     
        'merk' => $this->input->post('merk'),
        'namasupplier' => $this->input->post('namasupplier'),       
        'nokatalog' => $this->input->post('nokatalog'),     
        'noseri' => $this->input->post('noseri'),       
        'category' => $this->input->post('category'),
        'lokasi' => $this->input->post('lokasi'),
        'pengguna' => $this->input->post('pengguna'),
        'status' => $this->input->post('status'),
        'jadwalkal' => $this->input->post('jadwalkal'),     
        'manual' => $this->input->post('manual'),
        'dateinput' => $this->input->post('date'),      
        'foto' => $nama_foto
        //'created' => $tanggal
    );  
    $this->db->insert('tbdetail', $data);
}   
    public function save(){   
    $this->load->library('image_lib');
    //$nama_asli = $_FILES['userfile']['name'];
    $id = $this->input->post('id',TRUE);
    $config['file_name'] = $id ;//'_'.'_'.$nama_asli;
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png|jpeg|bmp';
    $config['max_size'] = '100000';
    $this->load->library('upload', $config);
    $this->upload->initialize($config);
    if ( ! $this->upload->do_upload('userfile'))
    {
        $files = $this->upload->data();
        $fileNameResize = $config['upload_path'].$config['file_name'];
        $size =  array(             
                    array('name'    => 'thumb','width'  => 100, 'height'    => 100, 'quality'   => '100%')
                );
        $resize = array();
        foreach($size as $r){               
            $resize = array(
                "width"         => $r['width'],
                "height"        => $r['height'],
                "quality"       => $r['quality'],
                "source_image"  => $fileNameResize,
                "new_image"     => base_url().$r['name'].'/'.$config['file_name']
            );
        $this->image_lib->initialize($resize); 
            if(!$this->image_lib->resize())                 
            die($this->image_lib->display_errors());
        }   
        $data = array('upload_data' => $this->upload->data());      
        $get_name = $this->upload->data();
        $nama_foto = $get_name['file_name'];
        $this->mcrud->savealat($nama_foto);
        redirect('instrument/detailalat');   

    }
    else
    {   
        //Moved your code up there
    }
}   

如果我是对的,问题是你把上传在else。试着移动代码,告诉我它是否有效

也许这个答案能帮到你。在评论部分有一些建议,可以为你找到一些解决办法。您的服务器不支持处理这类映像所需的GD功能。Ci