CodeIgniter Forums

Full Version: howto upload two files with different allowed_types
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Mitja[/eluser]
howto upload two files with different allowed_types. i need to upload two files, where first one must be an image (jpg, gif or png), the second file must be pdf.

How to check all two uploads?

If i use

Code:
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size']    = '1000';
$config['max_width']  = '1024';
$config['max_height']  = '768';

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

$config2['upload_path'] = './uploads/';
$config2['allowed_types'] = 'pdf';

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

What to do now?

$uploads = $this->upload->data() can not be used for boath files or i am wrong?

Thx for help

El Forum

[eluser]pistolPete[/eluser]
Have a look at the documentation:
Quote:$this->upload->do_upload()
Performs the upload based on the preferences you've set.
Note: By default the upload routine expects the file to come from a form field called userfile, and the form must be a "multipart type:

So all you have to do is call your two form fields differently, e.g. image and document, then process both form fields:
Code:
$image_data = array();
$document_data = array();

// load library only once
$this->load->library('upload');

// image configuration
$image_config['upload_path'] = './uploads/';
$image_config['allowed_types'] = 'gif|jpg|png';
$image_config['max_size']    = '1000';
$image_config['max_width']  = '1024';
$image_config['max_height']  = '768';

$this->upload->initialize($image_config);

// process image upload first
if ( ! $this->upload->do_upload('image'))
{
   // image upload error , display error etc.
}
// image was uploaded properly, continue
else
{
   $image_data = $this->upload->data();

   // document configuration
   $document_config['upload_path'] = './uploads/';
   $document_config['allowed_types'] = 'pdf';

   $this->upload->initialize($document_config);
  
   if ( ! $this->upload->do_upload('document'))
   {
      // document upload error , display error etc.
      // you might need to delete the former uploaded image file ...
   }
   else
   {
      $document_data = $this->upload->data();
   }
}

El Forum

[eluser]Colin Williams[/eluser]
You also don't need to load the library twice. Just use the initialize() function again (only after the first upload happens, of course).