Welcome Guest, Not a member yet? Register   Sign In
File upload: 2 files of different file types in same form
#1

[eluser]carvingCode[/eluser]
I need to upload 2 different files of different file types from a single form. One file will be a .csv file, the other a .doc file.

I can place 2 file fields in the form, but I have not been able to get CI to upload a file when the file field's name is something other than 'userfile'. Plus, I am unsure how to use CI upload library in this situation.

Anyone got a suggestion for how I might accomplish this?

TIA
#2

[eluser]carvingCode[/eluser]
Discovered how to use a different 'name' for the file field. (Of course, I missed this in the docs...) Sad

In the controller, pass the file field's name to the 'do_upload" method, like:

Code:
$this->upload->do_upload('myfile'))

This doesn't solve my problem, but may help me get closer. Guess I could always call 'do_upload' twice, with 2 separate configs, etc.?
#3

[eluser]carvingCode[/eluser]
Solved.

I passed in the name of the file field to 'do_upload'. I used $this->upload->initialize() to initialize the method with the settings for each filetype. Simply called 'do_upload' twice in my case.

Code:
// first file (CSV)

$config['upload_path'] = UPLOAD_PATH_CSV;
$config['allowed_types'] = 'csv';

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

if (!$this->upload->do_upload('mycsvfile')) {
  $msg = "<span class=\"error-msg\">" . $this->upload->display_errors() . "</span>";
} else {
  $upload_data = $this->upload->data();

  // do something with file
}

// second file (DOC)

$config2['upload_path'] = UPLOAD_PATH_DOC;
$config2['allowed_types'] = 'doc';

// no need to load upload library again
$this->upload->initialize($config2);

if (!$this->upload->do_upload('mydocfile')) {
  $msg = "<span class=\"error-msg\">" . $this->upload->display_errors() . "</span>";
} else {
  $upload_data = $this->upload->data();

  // do something with file
}




Theme © iAndrew 2016 - Forum software by © MyBB