php - CodeIgniter Muliptle File Upload -
this question has answer here:
- upload multiple files in codeigniter 7 answers
i'm new codeigniter, can me on muliptle image uploads. scenario is, validate each image if there errors, show each message below each input file. if there's no error upload , save file name database. need basic way on how this. i've tried no luck. appreciated. in advance!
this controller
hotel.php
public function add() { //if(!is_ajax_request()) return; $this->set_validation_rules(); if($this->form_validation->run()) { $m_insert = $this->get_posted_hotel_data(); $hotel_id = $this->hotel_model->insert($m_insert); $this->upload_image($hotel_id); redirect('hotel'); } else { $data['accept'] = array( 0 => 'false', 1 => 'true'); $data['destinations'] = prepare_dropdown_array($this->destination_model->get(), 'cde_id', 'cde_name', '--please select'); echo $this->load->view('form_hotel_add', $data); } } private function set_validation_rules() { $this->form_validation->set_rules('destination', '', 'required'); $this->form_validation->set_rules('stars', '', 'required|numeric'); $this->form_validation->set_rules('name', '', 'required'); $this->form_validation->set_rules('address', '', 'required'); $this->form_validation->set_rules('email', '', 'required|valid_email'); $this->form_validation->set_rules('phone', '', 'required|numeric'); $this->form_validation->set_rules('tm_comments', '', 'required'); $this->form_validation->set_rules('am_comments', '', 'required'); $this->form_validation->set_rules('accept[]', '', 'required'); $this->form_validation->set_rules('room', '', 'required'); $this->form_validation->set_rules('location', '', 'required'); $this->form_validation->set_rules('amenities', '', 'required'); $this->form_validation->set_rules('image1', 'image1', 'callback__handle_upload'); $this->form_validation->set_rules('image2', 'image2', 'callback__handle_upload'); $this->form_validation->set_rules('image3', 'image3', 'callback__handle_upload'); $this->form_validation->set_message('required', 'this field required.'); } private function get_posted_hotel_data() { $data1 = array('cho_destination' => $this->input->post('destination'), 'cho_stars' => $this->input->post('stars'), 'cho_name' => $this->input->post('name'), 'cho_address' => $this->input->post('address'), 'cho_email' => $this->input->post('email'), 'cho_phone' => $this->input->post('phone'), 'cho_tm_comment' => $this->input->post('tm_comments'), 'cho_am_comment' => $this->input->post('am_comments'), 'cho_rooms' => $this->input->post('room'), 'cho_location' => $this->input->post('location'), 'cho_amenities' => $this->input->post('amenities')); foreach($_post['accept'] $v) { $data2 = array("cho_accept" => $v); } $data = array_merge($data1, $data2); return $data; } private function upload_image($id) { $ctr=1; $x=1; foreach($_files $key => $value) { //print_r($_files); //die(); if(!empty($value['name'])) { $config['upload_path'] = './uploads/hotels'; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = '1024'; $config['encrypt_name'] = true; //$config['file_name'] = sprintf('%s_%s', $id, $value['name']); $filename=explode('.', $value['name']); $config['file_name'] = sprintf('%s_%s', $id, 'image_'.$ctr.'.'.$filename[1]); $config['overwrite'] = true; $this->load->library('upload', $config); if(move_uploaded_file($_files["image".$x]["tmp_name"],"uploads/hotels/" . $config['file_name'])) { $uploaded = $this->upload->data(); //create thumbnail $img_lib['image_library'] = 'gd2'; $img_lib['source_image'] = $uploaded['full_path']; $img_lib['master_dim'] = 'width'; $img_lib['quality'] = 75; $img_lib['maintain_ratio'] = true; $img_lib['width'] = 380; $img_lib['height'] = 280; $this->load->library('image_lib', $img_lib); $this->image_lib->clear(); $this->image_lib->initialize($img_lib); $this->image_lib->resize(); } } if(!empty($_files['image'.$x]['tmp_name'])) { $this->add_image($id, $config['file_name'], $x); } $ctr++; $x++; } } public function _handle_upload() { $y=1; foreach($_files $key=> $val) { if(empty($_files['image'.$y])) { $this->form_validation->set_message('_handle_upload', "you must upload image!"); return false; } $y++; } } private function add_image($id, $image, $x) { $data = array('cho_image'.$x => $image); $this->hotel_model->update($id, $data); }
and view: form_hotel_add.php
<div class="control-group"> <label for="image1" class="control-label">image 1</label> <div class="controls"> <input type="file" id="image1" name="image1" accept="image/*" /> <?=form_error('image1', '<br><label class="error">','</label>')?> </div> </div> <div class="control-group"> <label for="image2" class="control-label">image 2</label> <div class="controls"> <input type="file" id="image2" name="image2" accept="image/*"/> <?=form_error('image2', '<br><label class="error">','</label>')?> </div> </div> <div class="control-group"> <label for="image3" class="control-label">image 3</label> <div class="controls"> <input type="file" id="image3" name="image3" accept="image/*" /> <?=form_error('image3', '<br><label class="error">','</label>')?> </div> </div>
you need not use move_upload_file() use do_upload() method in upload class of codeigniter
Comments
Post a Comment