avenirer / MY_Upload

An addition to the CI_Upload, that allows for multiple files upload.
38 stars 23 forks source link

How can I pass the upload process if one of three fields of input files is not assigned any file #2

Closed Xoshbin closed 8 years ago

Xoshbin commented 8 years ago

Hi, I used this library and it works great, except only one issue because i made a change in the view page and I want it to be like that I just changed the view file like that to make three fields separately to use it for my database just like that:

                    echo '<div class="form-group">';
                    echo '<label for="img"> Main image: </label>';
                    echo form_upload('uploadedimages[]','','multiple');
                    echo '</div>';

                    echo '<div class="form-group">';
                    echo '<label for="img"> Second Image: </label>';
                    echo form_upload('uploadedimages[]','','multiple');
                    echo '</div>';

                    echo '<div class="form-group">';
                    echo '<label for="img"> Third Image: </label>';
                    echo form_upload('uploadedimages[]','','multiple');
                    echo '</div>';

This way above when I select files on all of the fields and upload it, it works great. but if I don't select a file in any one of them the process fails and it says " there is no file selected" something like that.

and this is my controller:

       function do_upload()
      {
    //if save button was clicked, get the data sent via post
    if ($this->input->server('REQUEST_METHOD') === 'POST')
        {
        //form validation
        $this->form_validation->set_rules('name', 'name', 'required');
        $this->form_validation->set_rules('manufacturer', 'manufacturer name', 'numeric');
        $this->form_validation->set_rules('model', 'model name');
        $this->form_validation->set_rules('category', 'category', 'required');
        $this->form_validation->set_rules('price', 'price', 'required|numeric');
        $this->form_validation->set_rules('stock', 'stock', 'required');
        $this->form_validation->set_rules('description', 'description');
        $this->form_validation->set_error_delimiters('<div class="alert alert-error"><a class="close" data-dismiss="alert">×</a><strong>', '</strong></div>');
        //if the form has passed through the validation
        if ($this->form_validation->run())
        {

            $config['upload_path'] = './uploads/products/temp';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '12200';
            $config['max_width']  = '12024';
            $config['max_height']  = '7268';

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

            if ( ! $this->upload->do_upload('uploadedimages'))
            {
                $error = array('error' => $this->upload->display_errors());

                $error['main_content'] = 'pages/add_product';
                $this->load->view('templates/template', $error);
            }
            else
            {
                $data = array('upload_data' => $this->upload->data());

                $data_to_store = array(
                'name'              => $this->input->post('name'),
                'manufacturers_id'  => $this->input->post('manufacturer'),
                'model'             => $this->input->post('model'),
                'categories_id'     => $this->input->post('category'),
                'sell_price'        => $this->input->post('price'),
                'stock'             => $this->input->post('stock'),
                'used'              => $this->input->post('used'),
                'description'       => $this->input->post('description'),
                'img'               => $data['upload_data'][0]['full_path'],
                'img1'              => $data['upload_data'][1]['full_path'],
                'img2'              => $data['upload_data'][2]['full_path'],
                'product_date'      => date('Y-m-d H:i:s'),
                'seller_id'         => $this->session->userdata('user_id'),
                'uploader_ip'       => $this->input->ip_address(),
                );
                //if the insert has returned true then we show the flash message
                if($this->products_model->store_product($data_to_store) == TRUE){
                    $this->session->set_flashdata('flash_message', 'Uploaded');

                    $data['main_content'] = 'pages/add_product';
                    $this->load->view('templates/template', $data);
                }else{
                    $this->session->set_flashdata('flash_message', 'not_uploaded');
                }
            }

        }//validation run
        else{
            $data['main_content'] = 'pages/add_product';
            $this->load->view('templates/template', $data);
        }

    }
    else{
            redirect('home/user_add_product'); 
        }
}
avenirer commented 8 years ago

as you can see in the user guide, there is one more config element named "multi" which by default is set to "all". You must set it to "ignore".