Welcome Guest, Not a member yet? Register   Sign In
How to Multiple File Upload with Insert Batch in CodeIgniter?
#1

So this is the problem, I'm trying to multi file upload in CodeIgniter but I want to also send it to two tables in database also.

AI = "auto_increment";
ci_posts: post_id(AI), post_image.
ci_relationship: id(AI), post_id.

So basically for every image uploaded to the ci_posts table, they should be submitted to the ci_relationship table as well.
Here is my controller:


PHP Code:
   public function newphoto($id){
 
       // Check Login
 
         if(!$this->session->userdata('user_id')){
 
            // Redirect to page
 
            redirect('users/login');
 
        }
 
    
         
// Field Rules
 
        $this->form_validation->set_rules('title''Title''trim');
 
    
         
if ($this->form_validation->run() == FALSE) {
 
    
             
// Set error message
 
            $this->session->set_flashdata('error''You must select one or more images');
 
    
             
// Redirect
 
            redirect('users/album/'.$id.'/'.$this->session->userdata('username'));
 
    
         
} else {
 
    
             
// Multiple File Upload
 
            $data = array();
 
            if($this->input->post('mysubmit') && !empty($_FILES['post_image']['name'])){
 
                $filesCount count($_FILES['post_image']['name']);
 
                for($i 0$i $filesCount$i++){
 
    
                     $file_name 
'post_image[]';
 
    
                     $_FILES
[$file_name]['name'] = $_FILES['post_image']['name'][$i];
 
                    $_FILES[$file_name]['type'] = $_FILES['post_image']['type'][$i];
 
                    $_FILES[$file_name]['tmp_name'] = $_FILES['post_image']['tmp_name'][$i];
 
                    $_FILES[$file_name]['error'] = $_FILES['post_image']['error'][$i];
 
                    $_FILES[$file_name]['size'] = $_FILES['post_image']['size'][$i];
 
    
                     
// Create user folder
 
                    $folder $this->session->userdata('username');
 
    
                     
// Upload Status Image
 
                    $config['upload_path'] = 'assets/img/users/media/'.$folder.'/';
 
                    $config['allowed_types'] = 'gif|jpg|png|jpeg';
 
                    $config['max_size'] = 0;
 
                    $config['encrypt_name'] = TRUE;
 
                    $config['remove_spaces'] = TRUE;
 
                    $config['detect_mime'] = TRUE;
 
                    $config['mod_mime_fix'] = TRUE;
 
    
                     $this
->load->library('upload'$config);
 
                    $this->upload->initialize($config);
 
    
                     
// If username folder does not exist, create it
 
                    if(!is_dir($config['upload_path'])) {
 
                        mkdir($config['upload_path'], 0777TRUE);
 
                    }
 
    
                     
// If files selected send them to ci_posts
 
                    if($this->upload->do_upload($file_name)){
 
    
                         $fileData 
$this->upload->data('file_name');
 
                        $upload_data[$i]['post_image'] = $fileData;
 
    
                         $this
->db->insert_batch('ci_posts'$upload_data);
 
    
                     
}
 
    
                     
// Get last post id for relationship data
 
                    $post_id $this->db->insert_id();
 
    
                     
// Relationship Array
 
                    $data[$i] = array(
 
                        'parent_id'   => $id,
 
                        'post_id'     => $post_id,
 
                        'user_id'     => $this->session->userdata('user_id'),
 
                        'status'      => 'published',
 
                        'type'        => 'user_media',
 
                    );
 
    
                     
// Insert relationship
 
                    $this->db->insert_batch('ci_relationship'$data);

 
                   // Activity Array
 
                   $data = array(
 
                       'resource_id' => $id,
 
                       'type'        => 'user_media',
 
                       'action'      => 'added',
 
                       'user_id'     => $this->session->userdata('user_id'),
 
                       'message'     => 'A new photo by (' $this->session->userdata('username') . ')'
 
                   );

 
                   // Insert Activity
 
                   $this->Activity_model->add($data);
 
    
                     
// Set Message
 
                    $this->session->set_flashdata('success''Photo has been added');
 
    
                     
// Redirect
 
                    redirect('users/album/'.$id.'/'.$this->session->userdata('username'));
 
    
                 
}
 
            }
 
        }
 
    

and here is my view:
PHP Code:
                   <?php
                        $action 
= array(
 
                       );
 
                       $attributes = array(
 
                           'id'    =>  'user_form',
 
                           'class' =>  ''
 
                       );
 
                       $hidden = array();
 
                   ?>
                    <?= form_open_multipart('users/newphoto/'.$album->post_id$attributes$hidden); ?>
                        <!-- <div id="extraInputsContainer"></div> -->
                        <div class="input-group">
                            <label class="input-group-btn">
                                <span class="btn btn-warning">
                                    Browse 
                                    <?php $data = array(
 
                                           'id'    =>  'post_image',
 
                                           'name'  =>  'post_image[]',
 
                                           'class' =>  'form-control',
 
                                           'value' =>  set_value('post_image'),
 
                                           'style' =>  'display:none',
 
                                       ); ?>
                                        <?= form_upload($data'''multiple'); ?>
                                </span>
                            </label>
                            <input type="text" class="form-control" readonly>
                        </div>
                        <hr>
                        <?php 
                            $args 
= array(
 
                               'id'    =>  'mysubmit',
 
                               'name'  =>  'mysubmit',
 
                               'class' =>  'btn btn-warning btn-sm',
 
                           );
 
                           $value html_escape('Submit');
 
                           $extra = array(
 
                               'onclick'   =>  '',
 
                           );
 
                       ?>
                        <?= form_submit($args$value$extra); ?>
                    <?= form_close(); ?>

Thanks in advance.

NOTE: The current output is one single image for every time I fill the form.
I do Front-End development most of the time 
Reply
#2

You need to start using Google Search and quit asking questions on here
when the answer is right in front of you.

I found this in like 5 seconds.

Upload Multiple Files in CodeIgniter with Example
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB