0

I have here multiple fields with multiple files uploads with array key name. I need to uploads multiple files per field and pass the filenames in array with keys into the model.

I really need this, hoping for someone would help me. Thanks.

Here's the example of my fields

//with 2 or more files
<input accept="application/pdf" name="vtc_file[0]" id="vtc_file0"  type="file" multiple/>
//with 2 or more files
<input accept="application/pdf" name="vtc_file[1]" id="vtc_file1"  type="file" multiple/>


<?php

$count = count($_FILES['vtc_file']['name']);
  for ($i=0; $i < $count; $i++)
  {
      foreach ($_FILES['vtc_file'] as $key1 => $value1)
      {
          foreach ($value1 as $key2 => $value2)
          {
              $files[$key2][$key1] = $value2;
          }
      }
  }
  $_FILES = $files;

  $document_config['upload_path'] = 'uploads/';  
  $document_config['allowed_types'] = 'pdf';
    $this->load->library('upload', $document_config);
    foreach ($_FILES as $fieldname => $fileObject)
    {
        if (!empty($fileObject['name']))
        {
            $this->upload->initialize($document_config);
            if (!$this->upload->do_upload($fieldname))
            {
                $errors = $this->upload->display_errors();
            }
            else
            {
                 $results = $this->models->save();
            }
        }
    }
5
  • what is the problem with your code ? Commented May 6, 2020 at 15:20
  • Doesn't upload multiple file per field Commented May 6, 2020 at 15:27
  • what is the purpose of your first for loop ? Commented May 6, 2020 at 15:44
  • check out this answer I think it should work just fine for you, maybe you will need to call twice for your 2 fields. Commented May 6, 2020 at 15:49
  • name="vtc_file[0]" but my filename used to have key Commented May 6, 2020 at 15:54

1 Answer 1

1

I've made a demo for you, I've explained the steps in the code itself. Hope it works for you.

View

<form action="<?Php echo base_url('home/myfunc'); ?>" method="POST" enctype="multipart/form-data">

    <!-- You need to make an array of input fields(See {name}) -->
    <input accept="application/pdf" name="vtc_file[0][]" id="vtc_file0"  type="file" multiple/>
    <!-- If the usdr uploads only one file it will be stored like vtc_file[0][1] = 'filename.pdf' and so on.-->
    <br>
    <input accept="application/pdf" name="vtc_file[1][]" id="vtc_file1"  type="file" multiple/><br>
    <button type="submit" value="submit">Submit</button>
</form>

Controller

function myfunc(){

    // check if the $_FILES is not empty(your validation here) then perform these actions↓↓
    foreach ($_FILES['vtc_file']['name'] as $key1 => $value1){  // We only need to loop through all the input fields(vtc_file)

        foreach ($value1 as $key2 => $value2){  // loop through name of all the files uploaded

            // Make a new dummy element(userfile) with the file(vtc_file') details in it
            $_FILES['userfile']['name']     = $_FILES['vtc_file']['name'][$key1][$key2];
            $_FILES['userfile']['type']     = $_FILES['vtc_file']['type'][$key1][$key2];
            $_FILES['userfile']['tmp_name'] = $_FILES['vtc_file']['tmp_name'][$key1][$key2];
            $_FILES['userfile']['error']    = $_FILES['vtc_file']['error'][$key1][$key2];
            $_FILES['userfile']['size']     = $_FILES['vtc_file']['size'][$key1][$key2];

            $config['upload_path']      = './uploads/aaa'; // path to the folder 
            $config['allowed_types']    = 'pdf';
            $config['max_size']         = 1000;
            // $config['max_width']    = 1024;
            // $config['max_height']   = 768;

            $this->load->library('upload', $config);    // load the {upload} library
            $this->upload->initialize($config);         // initialize the library

            if (!$this->upload->do_upload("userfile")){ // upload the file(current)

                $data['errors'][$key1][$key2] = $this->upload->display_errors();    // if any error store them in {errors} variable with keys

            }else{

                $upload_data = $this->upload->data();   // get the uploaded file data
                $data['filename'][$key1][$key2] = $upload_data['file_name'];    // store the upload file name in {filename} variable with keys
            }
        }
    }
    //load model {event_model}
    $this->load->model('event_model');
    $success = $this->event_model->save_file($data['filename']); // call model function

    // check if query successful
    if($success){
        // do something (Load a view)
        // You can show uploaded files in your view with $data['filename'][$key1][$key2]
        // And the errors of files that couldn't be uploaded with $data['errors'][$key1][$key2]
        echo '<pre>'; print_r($data['errors']);
    }else{
        // do something else
    }
}

Model

function save_file($data){

    echo '<pre>'; print_r($data);
    // Your insert query here.
    return true;
}

Output:

Model: $filename array

Array
(
    [0] => Array
        (
            [0] => Account_Software______Payment.pdf
            [1] => Account_Software______RTGS_Report.pdf
        )

    [1] => Array
        (
            [0] => BUSINESS_PROFILE.pdf
        )

)

Controller: $errors array

Array
(
     [1] => Array
         (
              [1] => 
        The filetype you are attempting to upload is not allowed.


          )

)

Images:
enter image description here

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

Comments

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.