php - 图片上传无法插入数据

标签 php codeigniter codeigniter-3

我将数据插入到上传图片到数据库中,运行成功,但是插入数据和上传图片时出现错误。

The path to the image is not correct.

Your server does not support the GD function required to process this type of image.

如果我插入数据而不上传图片不报错怎么办?图像数据库进入默认图像?

这是我的 Controller

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 中。尝试移动代码并告诉我它是否有效

关于php - 图片上传无法插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38909955/

相关文章:

php - PHP代码中的CSS效果

php - 使用 imap 和 php 检索最近的 3 封电子邮件

javascript - 在 laravel 5 中验证返回 false 时如何获取选项值的名称

php - 上传到域后,MySql 无法识别某些表列

php - Codeigniter 分页显示太多链接

codeigniter-3 - 消息 : call_user_func_array() expects parameter 1 to be a valid callback

php - 如何检查我的 SQL 更新是否已完成?

php - 如何在单个 json 中合并来自多个查询的多个结果

ajax 表单输入文件上传在 codeigniter 中加载单独的页面

php - CodeIgniter 添加和更新数据库没有成功,没有错误,没有,只是不会工作