-3

I'm a new with codeigniter, can anyone help me on muliptle image uploads. the scenario is, validate each image then if there are errors, show each message below each input file. if there's no error upload and save the file name to database. i really need a basic way on how to do this. I've tried anything but no luck. Any help is very much appreciated. thanks in advance!

this is my 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 is 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'] as $v)
                    {
                        $data2 = array("cho_accept" => $v);
                    }
        $data = array_merge($data1, $data2);
        return $data;   
    }

private function upload_image($id)
    {
        $ctr=1;
        $x=1;

        foreach($_FILES as $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 as $key=> $val)
        {
            if(empty($_FILES['image'.$y]))
            {
                $this->form_validation->set_message('_handle_upload', "You must upload an image!");
                return false;
            }

           $y++;
       }
    }

    private function add_image($id, $image, $x)
    {
        $data = array('cho_image'.$x => $image);
        $this->hotel_model->update($id, $data);
    }

And this is my 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>
0

1 Answer 1

0

you need not to use move_upload_file() use do_upload() method in upload class of codeigniter

Not the answer you're looking for? Browse other questions tagged or ask your own question.