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

(This post was last modified: 11-11-2018, 03:44 PM by HarrysR.)

(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.

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


Messages In This Thread
RE: Multiple file upload, resize and store in different tables. - by HarrysR - 11-11-2018, 03:18 PM



Theme © iAndrew 2016 - Forum software by © MyBB