Welcome Guest, Not a member yet? Register   Sign In
Multi Upload
#1

[eluser]the_unforgiven[/eluser]
Hi guys,

I have a function do upload which works great, it uploads and resizes just fine, but now I need to have the ability to upload 3 or 4 images instead of just one image.

here's the do upload function:
Code:
function _do_upload_file()
{
     //upload config
  $config = array(
   'allowed_types' => 'jpg|jpeg|gif|png|pdf',
   'upload_path' => $this->gallery_path,
   'max_size' => 6000,
   'overwrite' => false,
      'remove_spaces' => true
  );
  
     $this->load->library('upload', $config);
  $this->upload->initialize($config);
  
     if (!$this->upload->do_upload())
     {
         $this->form_validation->set_message('_do_upload_file', $this->upload->display_errors());
         return FALSE;
     }
     else
     {
  // Resize Config
   $config['image_library'] = 'gd2';
   $config['create_thumb'] = TRUE;
   $config['source_image'] = $this->upload->upload_path.$this->upload->file_name;
   $config['new_image'] = $this->gallery_path . '/thumbs';
   $config['maintain_ratio'] = TRUE;
   $config['thumb_marker'] = '_thumb';
   $config['width'] = 150;
         //echo $this->upload->upload_path.$this->upload->file_name . "<BR>";  
   $config['height'] = 100;
  
   $this->load->library('image_lib', $config);
   $this->image_lib->initialize($config);
   $this->image_lib->resize();
    
   if (!$this->image_lib->resize()){
          $this->form_validation->set_message('_do_upload_file', $this->upload->display_errors());              
   }
  
  }

}
Here's the model
Code:
function addproduct(){
  
  $data = array(  
   'name' => $this->input->post('name'),
   'short_description' => $this->input->post('short_description'),
   'long_description' => $this->input->post('long_description'),
   'status' => $this->input->post('status'),
   'category' => $this->input->post('category'),
   'featured' => $this->input->post('featured'),
   'price' => $this->input->post('price'),
   'code' => $this->input->post('code'),
   'supplier' => $this->input->post('supplier'),
   'supplier_code' => $this->input->post('supplier_code'),
   'weight' => $this->input->post('weight')
  
  );
  $image_data = $this->upload->data();
  $data['image'] = $image_data['file_name'];
  

  $this->db->insert('products', $data);
  redirect('admin/products');
  
  }

Any help, advice code examples highly appreciated.
Thanks in advance... Smile
#2

[eluser]BrokenLegGuy[/eluser]
I put all of my file input names into an array and pass it to my upload function. From there I use a foreach loop to process each field. Make sure you have

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

inside your loop or you'll only get the last file as a result.

Good luck
#3

[eluser]the_unforgiven[/eluser]
Dont suppose you could show me what you mean i know how loops work but not sure where to put in the code i already have?
#4

[eluser]BrokenLegGuy[/eluser]
I'll post it tonight for you. I just left work and I have a house to work on to get it to qualify for an FHA loan...
#5

[eluser]the_unforgiven[/eluser]
Ok cool, really appreciate it, thanks
#6

[eluser]BrokenLegGuy[/eluser]
Something like this... I found a minute

Code:
function do_upload($file_fields)
{
  $result_array = NULL;

  $config['upload_path']  = 'data';
  $config['allowed_types'] = '*';

  foreach($file_fields AS $k => $v)
  {
   $this->upload->initialize($config);
   $this->load->library('upload', $config);

   unset($actual_message[$v]);

   if($this->upload->do_upload($v) === FALSE)
   {
    $result_array[$v] = $this->upload->display_errors();
   }
   else
   {
    $result_array[$v] =  $this->upload->data();
   }
  }
  return $result_array;
}
#7

[eluser]the_unforgiven[/eluser]
would that fit in to what i posted in first post then?
#8

[eluser]BrokenLegGuy[/eluser]
with a little massaging, yes.




Theme © iAndrew 2016 - Forum software by © MyBB