无法使用其余输入数据上传图像

Can't upload image with the rest of the inputted data

我是 CodeIgniter 的新手,所以我只能利用框架可以提供的某些方面。在这种情况下,我想让用户上传一张包含他们其他信息的图像。我尝试将我创建的逻辑与我在此处找到的教程和信息相结合,但无济于事。使用此代码当前的方式,它将所有信息上传到服务器,但不会上传与用户输入的图像有关的任何内容。所以,我假设我在做一些致命的错误。任何建议表示赞赏。

    function adduser(){
        $config['upload_path'] = './uploads';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_width'] = '1024';
        $config['max_height'] = '1024';
        $this->load->library('upload', $config);

        $uinfo = $this->upload->data();
        $file = array(
            'img_name' => $uinfo['raw_name'],
            'ext' => $uinfo['file_ext']
        );
        $fullfile = $file['img_name'].$file['file_ext'];

        $data = array(
            'userid' => $this->session->userdata('userid'),
            'name' => $this->input->post('name'),
            'year' => $this->input->post('year'),
            'title' => $this->input->post('title'),
            'image' => $fullfile
        );
        $this->db->insert('user', $data);
        redirect('user/index');
}
    Try This

     function adduser(){
            $config['upload_path'] = './uploads';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_width'] = '1024';
            $config['max_height'] = '1024';
            $this->load->library('upload', $config);

            if (!$this->upload->do_upload('inputname')) {
                echo $this->upload->display_errors();
            } else {
                $uinfo = $this->upload->data();
                $file = array(
                    'img_name' => $uinfo['raw_name'],
                    'ext' => $uinfo['file_ext']
                );
                $fullfile = $file['img_name'].$file['file_ext'];

                $data = array(
                    'userid' => $this->session->userdata('userid'),
                    'name' => $this->input->post('name'),
                    'year' => $this->input->post('year'),
                    'title' => $this->input->post('title'),
                    'image' => $fullfile
                );
                $this->db->insert('user', $data);
                redirect('user/index');
            }
    }

Note: "inputname" is your HTML input file tag

请尝试以下操作

public function adduser(){
    {
             //This is the directory to which we upload our image
             //full permission is required for this folder
             //FCPATH is a constant defined in index.php - its the relative path to project root
             $config['upload_path']    = FCPATH.'uploads/';
             //Validation - Only gif jpg png extensions are allowed
             $config['allowed_types']  = 'gif|jpg|png';
             //unique file name
             $file_name           = time();
             $config['file_name'] = $file_name;
             $config['max_width'] = '1024';
             $config['max_height'] = '1024';

             $this->load->library('upload', $config);
             //checking file upload success

             if ( ! $this->upload->do_upload('image')) //here image is your file input name
             {
                //error in file uploading
                $message = $this->upload->display_errors();
                echo "<pre>";
                print_r($message);
                exit;
             }
             else
             {
                //successfully uploaded the image
                $message = 'file uploaded successfully';
                //getting full file upload information
                $result  = $this->upload->data();
                $data = array(
                    'userid' => $this->session->userdata('userid'),
                    'name'  => $this->input->post('name'),
                    'year'  => $this->input->post('year'),
                    'title' => $this->input->post('title'),
                    'image' => $result['file_name']
                );
                $this->db->insert('user', $data);

             }

            redirect('user/index');
    }

在上面我使用了 "image" 作为文件输入字段名称