Welcome Guest, Not a member yet? Register   Sign In
Multiple file upload, resize and store in different tables.
#1

Hello...again!!

I've created this controller where i'm trying to upload multiple image files and i want to store them in two different tables (the first as the main image in "table_1" and the others as extra images in "table_2".

The thing is that i does not store the other ones and now i get an error like "Undefined variable $image" as you will see in the example.
Any ideas on that?!

Controller
Code:
 public function submit(){
   if ($this->session->userdata('logged_in') == true) {
     $user_id = $this->session->userdata('user_id');
     $data['user'] = $this->user_model->get_user_data($user_id);
   }
   $data['categories'] = $this->pet_model->get_pet_category();
   $data['sizes'] = $this->pet_model->get_pet_sizes();

   $this->form_validation->set_rules('petTitle', 'Title, 'required');
   $this->form_validation->set_rules('pet_category', 'Category', 'required');

   if($this->form_validation->run() === FALSE){
     echo validation_errors();
   } else {
     // Single
     $pet_entry_id = random_string('numeric', 6);

     $dir_exist = false; //Directory exist
     if ( !is_dir('assets/img/pets/' . $pet_entry_id) ) {
       mkdir('./assets/img/pets/' . $pet_entry_id, 0777, true);
       $dir_exist = true; // dir not exist
     }

     // Multiple
     if( !empty($_FILES['images']['name']) ){

       $countfiles = count($_FILES['images']);
       for($i=0; $i<1; $i++){
         if(!empty($_FILES['images']['name'][$i])){
           $_FILES['image']['name'] = $_FILES['images']['name'][$i];
           $_FILES['image']['type'] = $_FILES['images']['type'][$i];
           $_FILES['image']['tmp_name'] = $_FILES['images']['tmp_name'][$i];
           $_FILES['image']['error'] = $_FILES['images']['error'][$i];
           $_FILES['image']['size'] = $_FILES['images']['size'][$i];

           $config['upload_path'] = './assets/img/pets/' . $pet_entry_id;
           $config['encrypt_name'] = TRUE;
           $config['allowed_types'] = 'gif|jpg|jpeg|png';
           $config['max_size'] = '1310720';
           $config['max_width'] = '2048';
           $config['max_height'] = '2048';

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

           if ( $this->upload->do_upload('image') ) {
             $image_data = $this->upload->data();
             $config['image_library'] = 'gd2';
             $config['source_image'] = $image_data['full_path'];
             $config['maintain_ratio'] = TRUE;
             $config['width'] = 650;
             $config['height'] = 650;

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

             $image = $image_data['file_name'];
           }
         }
       }

       for($i=1; $i<$countfiles; $i++){
         if(!empty($_FILES['images']['name'][$i])){
           $_FILES['image']['name'] = $_FILES['images']['name'][$i];
           $_FILES['image']['type'] = $_FILES['images']['type'][$i];
           $_FILES['image']['tmp_name'] = $_FILES['images']['tmp_name'][$i];
           $_FILES['image']['error'] = $_FILES['images']['error'][$i];
           $_FILES['image']['size'] = $_FILES['images']['size'][$i];

           $config['upload_path'] = './assets/img/pets/' . $pet_entry_id;
           $config['encrypt_name'] = TRUE;
           $config['allowed_types'] = 'gif|jpg|jpeg|png';
           $config['max_size'] = '1310720';
           $config['max_width'] = '2048';
           $config['max_height'] = '2048';

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

           if ( $this->upload->do_upload('image') ) {
             $image_data = $this->upload->data();
             $config['image_library'] = 'gd2';
             $config['source_image'] = $image_data['full_path'];
             $config['maintain_ratio'] = TRUE;
             $config['width'] = 650;
             $config['height'] = 650;

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

             $m_image = $image_data['file_name'];
             $this->pet_model->insert_images($m_image, $pet_entry_id);
           }
         }
       }

     } else {
       if($dir_exist)
       rmdir('./assets/img/pets/' . $pet_entry_id);
       $image = 'default_petavatar.png';
     }

     $this->pet_model->create_pet($image, $pet_entry_id);
     redirect('pets');
   }

   $this->render_page('pets/submit', $data);
 }


File Input
Code:
<div class="row">
 <div class="col-md-12">
 <input class="form-control" type="file" name="images[]" id="images" multiple value="">
 </div>
</div>


"Insert images model
Code:
public function insert_images($m_image, $pet_entry_id){

$data['image_path'] = $m_image;
$data['pet_entry_id_img'] = $pet_entry_id;

return $this->db->insert('pet_images', $data);
}



P.S.: If you have any better (or maybe safer) way on how to achieve this feel free to recommend!!

Thank you!

//Life motto
if (sad() == true) {
     sad().stop();
     develop();
}
Reply


Messages In This Thread
Multiple file upload, resize and store in different tables. - by HarrysR - 11-04-2018, 05:29 PM



Theme © iAndrew 2016 - Forum software by © MyBB