CodeIgniter Forums
Problems with Multiple File Upload Functions - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Problems with Multiple File Upload Functions (/showthread.php?tid=57918)



Problems with Multiple File Upload Functions - El Forum - 04-25-2013

[eluser]mpar612[/eluser]
Hi Everyone,

I have the two functions in the code below to upload files. artist_upload is to upload an image, bio_upload is to upload a Word doc or pdf. Both of these functions work well independently, but when I put them together the first one (artist_upload) works fine, but bio_upload kicks out an error. By doing a print_r($this->upload->data('file_name')) on the file uploaded for bio_upload, I see that the $this->upload->data('file_name') is combining elements of the artist_upload image and the bio_upload.

I am still pretty new to codeigniter. Does anyone have any thought on how to resolve this? Is there a better way for me to go about doing this? I have a dozen more file uploads to add to this same page, so it's crucial that I have this working properly.

Thanks in advance!

Code:
public function artist_upload()
{
  $config['upload_path'] = './uploads/';
  $config['allowed_types'] = 'gif|jpg|png';
  $config['max_size'] = '70000';
  $config['max_width']  = '160';
  $config['max_height']  = '100';
  
  $this->load->library('upload', $config);

if (isset($_FILES['artist_image']) && !empty($_FILES['artist_image']['name']))
  {
   if ($this->upload->do_upload('artist_image'))
   {
    // set a $_POST value for file for use in db query
    $_POST['artist_image'] = $this->upload->data('file_name');
    return true;
   }
   else
   {
    // display error because does not meet $config requirements
    $this->form_validation->set_message('artist_upload', $this->upload->display_errors());
    return false;
   }
  }
  else
  {
   //display error because nothing was uploaded
   $this->form_validation->set_message('artist_upload', "You must upload a .gif, .jpg or .png");
   return false;
  }
}

public function bio_upload()
{
  $config['upload_path'] = './uploads/';
  $config['allowed_types'] = '*';
  //$config['allowed_types'] = 'application/msword|application/vnd.openxmlformats-officedocument.wordprocessingml.document|pdf';
  $config['max_size'] = '70000';
  
  $this->load->library('upload', $config);

if (isset($_FILES['bio']) && !empty($_FILES['bio']['name']))
  {
   if ($this->upload->do_upload('bio'))
   {
    // set a $_POST value for file for use in db query
    $_POST['bio'] = $this->upload->data('file_name');
    return true;
   }
   else
   {
    // display error because does not meet $config requirements
    print_r($this->upload->data('file_name'));
    $this->form_validation->set_message('bio_upload', $this->upload->display_errors());
    return false;
   }
  }
  else
  {
   //display error because nothing was uploaded
   $this->form_validation->set_message('bio_upload_', "You must upload a .doc, .docx, or .pdf");
   return false;
  }
}



Problems with Multiple File Upload Functions - El Forum - 04-26-2013

[eluser]TheFuzzy0ne[/eluser]
When you say "it kicks out an error", can you be more specific? What's the error you're getting?

On thing that does spring to mind, is that you should be using $this->upload->initialize(). The second time you load the library, since it's already loaded, it's not loaded again. I'd suggest loading the library in your controller, and then calling $this->upload->initialize($config) from within your validation methods.


Problems with Multiple File Upload Functions - El Forum - 04-26-2013

[eluser]mpar612[/eluser]
Thank you - $this->upload->initialize() cleared up the issue.

Just in case anyone else has the same issue, my error was "The filetype you are attempting to upload is not allowed".