文件上传不起作用 - Codeigniter

时间:2017-09-11 09:13:19

标签: php codeigniter upload

我尝试上传图片,但是在添加上传部分代码之前它没有用     工作正常,表格中的所有条目都是     正确存储在数据库中     但现在形式     文本字段也不起作用..

CONTROLLER -
  我试过下面的代码。但它没有工作。请有人帮我解决这个问题吗?

public function index(){
                            // if(!$this->session->userdata('logged_in')){
                            //  redirect('register/login');
                            // }
                            $this->form_validation->set_rules('name', 'Name', 'required');
                            $this->form_validation->set_rules('location', 'Location', 'required');
                            $this->form_validation->set_rules('dob', 'DOB', 'required');
                            $this->form_validation->set_rules('height', 'Height', 'required');
                            $this->form_validation->set_rules('weight', 'Weight', 'required');
                            $this->form_validation->set_rules('optselect', 'Complexion', 'required');
                            $this->form_validation->set_rules('email', 'Email already exists.', 'required');

                            if($this->form_validation->run() === FALSE){

                                $this->load->view('templates/pheader');
                                $this->load->view('profile/aboutme');
                                $this->load->view('templates/pfooter');
                            } else {
                                //Upload Image
                                $path = './assets/uploads/profilepic';
                                $config['upload_path'] = $path;
                                $config['allowed_types'] = 'gif|jpg|png';
                                $config['max_size'] = 15000;
                                $config['max_width'] = 1920;
                                $config['max_height'] = 1080;

                                $this->load->library('upload', $config);

                                if(!$this->upload->do_upload()){
                                    echo "lol";
                                    echo $this->upload->display_errors();
                                    $this->session->set_flashdata('file_error', $this->upload->display_errors());
                                    $post_image = 'noimage.jpg';
                                    exit();
                                }else{
                                    $data = array('upload_data' => $this->upload->data());
                                    $post_image = $_FILES['userfile']['name'];
                                }

                                $this->profile_model->about($post_image);
                                redirect('profile/vabout');
                            }
                        }

查看 -

                         <div class="form-group">
                            <label class="col-md-3 text-right">Upload Profile Pic:</label>
                            <div class="col-md-9">
                            <input type="file" name="userfile" id="userfile" size="20" />
                            </div>
                    </div>

MODEL -

public function about($post_image){
                        $data = array(
                                'name' => $this->input->post('name'),
                                'location'=>$this->input->post('location'),                 
                                'dob' => $this->input->post('dob'),
                                'optradio' => $this->input->post('optradio'),
                                'height'=>$this->input->post('height'),
                                'weight'=>$this->input->post('weight'),
                                'optselect'=>$this->input->post('optselect'),
                                'email' => $this->input->post('email'),
                                'web'=>$this->input->post('web'),
                                'awards'=>$this->input->post('awards'),
                                'cw'=>$this->input->post('cw'),
                                'pp'=>$this->input->post('pp'),
                                'youtube'=>$this->input->post('youtube'),
                                'fb'=>$this->input->post('fb'),
                                'ld'=>$this->input->post('ld'),
                                'tw'=>$this->input->post('tw'),
                                'insta'=>$this->input->post('insta'),
                                'image'=> $post_image
                            );

                        return $this->db->insert('profile', $data);
                    }

1 个答案:

答案 0 :(得分:0)

试试这个

<form enctype="multipart/form-data" method="post" >