0

i am uploading four images from my form but the images are not uploading. I pasted my code below. Please correct me where i done mistake. first i did validation and then i configured the and path. Later i loaded upload library and later given image path for each image.

my Controller Code

public function upro()
    {       
        $this->form_validation->set_rules('pro_name','Product','required');
        $this->form_validation->set_rules('pro_image1','Image1','required');
        $this->form_validation->set_rules('pro_image2','Image2','required');
        $this->form_validation->set_rules('pro_image3','Image3','required');
        $this->form_validation->set_rules('pro_image4','Image4','required');
        // $today = date('Y-m-d');


        if($this->form_validation->run()){
            function uploadPic()
        {
            $config=[
                'upload_path' => './uploads',
                'allowed_types' => 'jpg|gif|png|jpeg'
            ];
            $this->load->library('upload',$config);
        }
            $data = $this->input->post();
            $today = date('Y-m-d');
            $data['pro_date'] = $today;
            $info = $this->upload->data();
            $image_path = base_url("uploads/".$info['raw_name'].$info['file_ext']);
            $data['pro_image1'] = $image_path;
            $data['pro_image2'] = $image_path;
            $data['pro_image3'] = $image_path;
            $data['pro_image4'] = $image_path;
            unset($data['submit']);
            $this->adata->uproQ($data);
            $this->session->set_flashdata('msg','Product uplaod success');
            return redirect('admin/products');

        }else{
            $this->session->set_flashdata('msg','product uplaod failed');
            return redirect('admin/apro');
        }
    }

my model Code

public function uproQ($data)
    {
            return $this->db->insert('products',$data);
    }

my form view Code

<?php echo form_open_multipart('admin/upro');?>
<label><h5>product Name:*</h5></label>
<?php echo form_input(['name'=>'pro_name','class'=>'form-control','placeholder'=>'product Name Here','value'=>set_value('pro_name')]);?>
<?php echo form_upload(['name'=>'pro_image1']);?>
<label><h5>product Image2:*</h5></label>
<?php echo form_upload(['name'=>'pro_image2']);?>
<label><h5>product Image3:*</h5></label>
<?php echo form_upload(['name'=>'pro_image3']);?>
<label><h5>product Image4:*</h5></label>
<?php echo form_upload(['name'=>'pro_image4']);?>
<button type="reset" class="btn btn-warning">Reset</button> <button type="submit" class="btn btn-primary">Submit</button><hr>
<?php form_close();?>
  • bad practice to save image into database, it will slower down your queries, I suggest you to directly save files into respective folders and while fetching image url, just check if file exists or not – Rahul Jan 26 '18 at 03:26
  • whats this `$this->adata->uproQ($data);`, Oh is `uproQ` in another file, just saw the comment `//my model Code`, that was somewhat unclear. I would suggest doing separate code blocks for the question with the comments in real text. – ArtisticPhoenix Jan 26 '18 at 03:27
  • i am passing all this form data into the my model function uproQ($data) – Sachin Singh Jan 26 '18 at 03:30
  • Right, I just re-formatted your question a bit, hope you don't mind. It's a bit cleaner to read it this way "at a glance" At first I thought it was the same file and I was like – ArtisticPhoenix Jan 26 '18 at 03:30
  • Bro if you know the right way to upload tell me the modifications for this code to upload pics – Sachin Singh Jan 26 '18 at 03:31
  • Bro, what version of CI is this? – ArtisticPhoenix Jan 26 '18 at 03:33
  • I can tell you one thing, you are using the same path for all 4 images `$image_path = base_url("uploads/".$info['raw_name'].$info['file_ext']);` probably not what you want. And it looks like this is never called `uploadPic()` – ArtisticPhoenix Jan 26 '18 at 03:34
  • Dear phoenix yup i want to store all images in one folder so i given that path. if i am wrong how can i do that.i am using version of CI 3.x... – Sachin Singh Jan 26 '18 at 03:37
  • That may be but that is not a folder path, as it has a filename and extension. This also seems wrong `function uploadPic() {...}` a. it's never called, b. even if it was what does it do, load the upload class in a separate scope?, c. if you call the method it's in more then once `upro`, it will issue an error for a previously defined function, because this is not a closure `$uploadPic = function(){..};` for example. – ArtisticPhoenix Jan 26 '18 at 03:39
  • I suggest consoling the DOCs, https://www.codeigniter.com/userguide3/libraries/file_uploading.html Something about `$this->upload->do_upload('userfile')` seems missing... hint hint – ArtisticPhoenix Jan 26 '18 at 03:43
  • You are trying to upload multiple images with the same name field google search upload multiple files with codeigniter –  Jan 26 '18 at 03:50
  • ok Dear bro i will do it – Sachin Singh Jan 26 '18 at 03:56
  • @SachinSingh method inside method `f($this->form_validation->run()){ function uploadPic()` – Abdulla Nilam Jan 26 '18 at 04:55

1 Answers1

1

Aside from the method inside the method, and aside from never calling $this->upload->do_upload('name_of_input'), the upload class can only upload one image at a time, you need a for loop for your files array. Also you cannot use form validation for image uploads, form validation only works for $_post fields not $_files fields. I'm surprised this isn't giving you an error that the fields aren't present.

$this->load->library('form_validation');
$this->form_validation->set_rules('pro_name', 'Product', 'required');
$expected_files = array('pro_image1', 'pro_image2', 'pro_image3', 'pro_image4');
//https://stackoverflow.com/questions/12289225/codeigniter-file-upload-required-validation
$i = 1;
foreach ($expected_files as $field_name) {
    if (empty($_FILES[$field_name]['name'])) {
        $this->form_validation->set_rules($field_name, 'Image' . $i, 'required');
    }
    $i++;
}
if ($this->form_validation->run()) {
    $config = [
        'upload_path' => './uploads',
        'allowed_types' => 'jpg|gif|png|jpeg',
    ];
    $this->load->library('upload', $config);
    $data = array();
    foreach ($_FILES as $field_name => $field_values) {
        if (!in_array($field_name, $expected_files)) {
            continue; // just in case user tries to add more upload fields
        }
        $this->upload->do_upload($field_name);
        $info = $this->upload->data();
        $image_path = base_url("uploads/" . $info['raw_name'] . $info['file_ext']);
        $data[$field_name] = $image_path;
    }
    $today = date('Y-m-d');
    $data['pro_date'] = $today;
    $data['pro_name'] = $this->input->post('pro_name');
    $this->adata->uproQ($data);
    $this->session->set_flashdata('msg', 'Product upload success!');
    return redirect('admin/products');
} else {
    $this->session->set_flashdata('msg', validation_errors());
    return redirect('admin/apro');
}
Alex
  • 9,215
  • 8
  • 39
  • 82
  • Dear alex it worked the files uploaded to the uploads folder but the image names not inserted into the database fields. so the files are not able to fetch because it doesn't have images names in database. how i have to solve this – Sachin Singh Jan 26 '18 at 10:56
  • It should work `$data[$field_name] = $image_path;` equates to `$data['pro_image1'] = $image_path;` can you `print_r($data)` before `$this->adata->uproQ($data);`? – Alex Jan 26 '18 at 19:45
  • Array ( [pro_name] => black skirt of girl [pro_cat] => skirt [pro_pa_cat] => women [pro_desc] => Lorem Ipsum is simply dummy text of the printing and typesetting industry.er including versions of Lorem Ipsum. [pro_color] => black [pro_cost] => 655 [pro_brand] => american eagle [pro_date] => 2018-01-27 ) – Sachin Singh Jan 27 '18 at 04:00
  • My guess is you are overwriting the array with more data as you have post data in your printr that wasn't in the question and I am dubious as to how you are adding it. My logic is sound I've checked it over again. If the files are getting uploaded than the array should be populated with the data for the images. – Alex Jan 27 '18 at 18:43