CodeIgniter Forums
Multiple file upload, resize and store in different tables. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: Multiple file upload, resize and store in different tables. (/showthread.php?tid=72093)



Multiple file upload, resize and store in different tables. - HarrysR - 11-04-2018

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!


RE: Multiple file upload, resize and store in different tables. - jreklund - 11-05-2018

Personally I would rewrite the insert_images too and make use of insert_batch instead, so that you don't need to make a foreach loop.
PHP 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_id0777true);
       
$dir_exist true// dir not exist
     
}

     
// Multiple
     
$images = array();
     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);

             
$images[] = $image_data['file_name'];
           }
         }
       }
     } else {
       if(
$dir_exist)
       
rmdir('./assets/img/pets/' $pet_entry_id);
       
$image 'default_petavatar.png';
     }
     if( ! empty(
$images) )
     {
         
$image array_shift($images);
         
$this->pet_model->create_pet($image$pet_entry_id);
         if( ! empty(
$images) )
         {
             foreach(
$images AS $img)
             {
                
$this->pet_model->insert_images($img$pet_entry_id);
             }
         }
     }

     
redirect('pets');
   }

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



RE: Multiple file upload, resize and store in different tables. - HarrysR - 11-11-2018

(11-05-2018, 01:08 PM)jreklund Wrote: Personally I would rewrite the insert_images too and make use of insert_batch instead, so that you don't need to make a foreach loop.
PHP 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_id0777true);
 
      $dir_exist true// dir not exist
 
    }

 
    // Multiple
 
    $images = array();
 
    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);

 
            $images[] = $image_data['file_name'];
 
          }
 
        }
 
      }
 
    } else {
 
      if($dir_exist)
 
      rmdir('./assets/img/pets/' $pet_entry_id);
 
      $image 'default_petavatar.png';
 
    }
     if( ! empty(
$images) )
     {
         
$image array_shift($images);
         
$this->pet_model->create_pet($image$pet_entry_id);
         if( ! empty(
$images) )
         {
             foreach(
$images AS $img)
             {
                
$this->pet_model->insert_images($img$pet_entry_id);
             }
         }
     }

 
    redirect('pets');
 
  }

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

Sorry for being late to answer but i was trying to insert it in my code and clean some other parts of the code.
One issue is that while it uploads the first image of the array, the others do not pass in the "foreach" loop and they are not uploaded in db neither in the folder..
Any ideas?!

[EDIT]: I'm just an idiot! I had the number "1" instead of the countfiles number in the "for" loop! It works perfectly! May i ask if you know any good plugins for previewing - deleting - editing images before uploading? I've searched all over the internet and foudn nothing.


RE: Multiple file upload, resize and store in different tables. - jreklund - 11-12-2018

I'm afraid I don't have any tips there. But it's possible. You can make a Drag and Drop uploader/previewer in Javascript, you don't need to upload them. But I don't know if it's possible to drop one image and later drop five more and not delete them. It should be possible to store them in a variable.

I'm using this design together with Dropzone JS.
https://stackoverflow.com/a/47178466

But I won't them to be automatically uploaded.


RE: Multiple file upload, resize and store in different tables. - HarrysR - 11-12-2018

(11-12-2018, 10:28 AM)jreklund Wrote: I'm afraid I don't have any tips there. But it's possible. You can make a Drag and Drop uploader/previewer in Javascript, you don't need to upload them. But I don't know if it's possible to drop one image and later drop five more and not delete them. It should be possible to store them in a variable.

I'm using this design together with Dropzone JS.
https://stackoverflow.com/a/47178466

But I won't them to be automatically uploaded.

Thank you very much! I will take a look at it!  Smile