1

I have a problem with how to upload 2 file input in 1 form CI 3 with different file name. My view code: There is have 2 file input, 1. file_sertifikat and 2. file_rekomendasi

<div class="form-group">
<label for="file_sertifikat">File Sertifikat</label>
<input type="file" name="file_sertifikat" class="form-control" id="file_sertifikat">
<small class="text-danger"><?= form_error('file_sertifikat') ?></small>
</div>

<div class="form-group">
<label for="file_rekomendasi">File Rekomendasi</label>
<input type="file" name="file_rekomendasi" class="form-control" id="file_rekomendasi">
<small class="text-danger"><?= form_error('file_rekomendasi') ?></small>
</div>

My Controller action to save filename to database:

$file_sertifikat = $this->upload_file_sertifikat($this->input->post('file_sertifikat'));
    $file_rekomendasi = $this->upload_file_rekomendasi($this->input->post('file_rekomendasi'));

    $data = array(
      'file_sertifikat' => $file_sertifikat,
      'file_rekomendasi' => $file_rekomendasi,
    );

    $this->perizinan_model->insert_data($data,'perizinan');
    redirect('admin/perizinan');
    }

and my upload file function:

        public function upload_file_sertifikat(){
            $config['upload_path'] = './assets/uploads';
            $config['file_name'] = 'file_sertifikat_'.rand(1, 99999);
            $config['allowed_types'] = 'gif|jpg|png|jpeg|pdf';
            $config['remove_space'] = TRUE;
            $config['max_size'] = 10048;

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

            if($this->upload->do_upload('file_sertifikat')){
                    return $this->upload->data("file_name");  
            }else{
                    return "defaultimg.png";
            }
        }

        public function upload_file_rekomendasi(){
            $config['upload_path'] = './assets/uploads';
            $config['file_name'] = 'file_rekomendasi_'.rand(1, 99999);
            $config['allowed_types'] = 'gif|jpg|png|jpeg|pdf';
            $config['remove_space'] = TRUE;
            $config['max_size'] = 10048;

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

            if($this->upload->do_upload('file_rekomendasi')){
                    return $this->upload->data("file_name");  
            }else{
                    return "defaultimg.png";
            }
        }

I want to upload that 2 different filename one by one, but my code overwrite the file. Thank before.

2
  • try this upload multiple files ci3 Commented Dec 12, 2021 at 6:22
  • @SherifSalah i have see that, but at there is with array, while i want to upload the file different field Commented Dec 12, 2021 at 6:34

1 Answer 1

0

In html change

<input type="file" name="file_rekomendasi" class="form-control" id="file_rekomendasi">

to

<input type="file" name="file_rekomendasi[]" class="form-control" id="file_rekomendasi" multiple >

In the controller, you will get $file_rekomendasi as an array. Handel that as like array. Run a loop.

Sign up to request clarification or add additional context in comments.

2 Comments

But there is have 2 different field, file_rekomendasi and file_sertifikat
for multiple insert arrays, you have to generate the array from the input field array. Just run loop inside loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.