1

Controller

public function insertDepartmentImage()
{
    $this->load->model('Gallery_Model/Gallery_Model');  
    $config['upload_path'] = './uploads/galleryImg';
    $config['allowed_types'] = 'png|jpg|gif|jpeg';
            $config['max_size']    = '10000000';             
    $config['file_name']='smallImage';
    $config['file_name1']='bigImage';
    $config['overwrite']=false;   
    $this->load->library('upload', $config);        
    if (!is_dir('uploads'))
    {
        mkdir('./uploads', 0777, true);
    }
    $dir_exist = true;  
    if (!is_dir('uploads/' . $album))
    {
    mkdir('./uploads/' . $album, 0777, true);
    $dir_exist = false; // dir not exist
    }
    else{ }              

    if (!$this->upload->do_upload('filename'))
    {
        $this->load->model('Gallery_Model/Gallery_Model');            
        $upload_error = array('error' => $this->upload->display_errors());       
        $this->load->view('/admin/Gallery/vwGallery',$upload_error);
    }
    else
    {   
        $upload_data = $this->upload->data();   
        $data['success_msg'] = '<div class="alert alert-success text-center">Your file <strong>' . $upload_data['file_name'] . '</strong> was successfully uploaded!</div>';        
        $this->load->model('Gallery_Model/Gallery_Model');               
        $file_name =   $upload_data['file_name'];
        $file_name1 =   $upload_data['file_name1'];
        $deptId = $this->input->post('deptId'); 
        $deptDetails = $this->input->post('deptDetails');
        $deptDetails1 = $this->input->post('deptDetails1');      
        $this->Gallery_Model->insertDepartmentImage($deptId,$file_name,$file_name1,$deptDetails,$deptDetails1);           
    }       
} 

It work fine but uploading only one filename into database. I have to upload one more image into database. But its not working.For Uploading Another image what to do for this.

In my view I have given two input field <input id="file-0" name="filename" class="file" type="file" multiple="true" />

<input id="file-0" name="filename1" class="file" type="file" multiple="true" />
3
  • where is your second file code? Commented Oct 31, 2015 at 7:59
  • Actually I think problem is in Controller Code. Commented Oct 31, 2015 at 9:00
  • 1
    make an arrays [] name="filename[]" and do_upload('filename[]') see this Commented Oct 31, 2015 at 9:03

4 Answers 4

3

Thank you guys. But I will just changing code into Controller and It's worked for me.

public function insertDepartmentImage()
{
     $m = $_FILES['filename']['name'];      
     $n = $_FILES['filename1']['name']; 
    if ($m !== "")
    {
        $config['upload_path'] = './uploads/galleryImg';                        
        $config['log_threshold'] = 1;
        $config['allowed_types'] = 'jpg|png|jpeg|gif';
        $config['max_size'] = '100000'; // 0 = no file size limit
        $config['file_name']='smallImage';          
        $config['overwrite'] = false;
        $this->load->library('upload', $config);
         $this->upload->do_upload('filename');
         $upload_data = $this->upload->data();
         $file_name = $upload_data['file_name'];
 }          
 if ($n !== "")
    {
        $config['file_name']='bigImage';
        $config['upload_path'] = './uploads/galleryImg';             
        $config['allowed_types'] = 'jpg|png|jpeg|gif';
        $config['max_size'] = '100000'; // 0 = no file size limit           
        $config['overwrite'] = false;
        $this->load->library('upload', $config);
        $this->upload->do_upload('filename1');
        $upload_data = $this->upload->data();
        $file_name1 = $upload_data['file_name']; 
    } 


        $deptId = $this->input->post('deptId'); 
        $deptDetails = $this->input->post('deptDetails');
        $deptDetails1 = $this->input->post('deptDetails1');

        $this->Gallery_Model->insertDepartmentImage($deptId,$file_name,$file_name1,$deptDetails,$deptDetails1); 

}/* closed insertDepartmentImage*/      

View

<input id="file-0" name="filename" class="file" type="file" multiple="true" />
<input id="file-0" name="filename1" class="file" type="file" multiple="true" />
Sign up to request clarification or add additional context in comments.

1 Comment

ji from your answer i try to insert 2 text, 2 images in table but i am getting smallImage in <input type="file"> for both the images not getting the right image name.
0

By default codeIgniter doesn't support multi-file upload. So you can use this library CodeIgniter Multiple Upload Library

https://github.com/anuragrath/CodeIgniter-Multiple-File-Upload

Comments

0

Thank you guys... I solve my problem now...

Here are my code, hope it can help

public function dashboard_templatedefault(){

    $upload1 = $_FILES['upload1']['name'];
    $upload2 = $_FILES['upload2']['name'];
    $upload3 = $_FILES['upload3']['name'];
    $upload4 = $_FILES['upload4']['name'];
    $upload5 = $_FILES['upload5']['name'];
    $upload6 = $_FILES['upload6']['name'];
    $upload7 = $_FILES['upload7']['name'];
    $upload8 = $_FILES['upload8']['name'];

    if($upload1 !== ""){

        $config['upload_path'] = './upload/';                        
        $config['log_threshold'] = 1;
        $config['allowed_types'] = 'jpg|png|jpeg|gif';
        $config['max_size'] = '0'; // 0 = no file size limit
        $config['file_name']='upload1';          
        $config['overwrite'] = false;
         $this->load->library('upload', $config);
         $this->upload->do_upload('upload1');
         $upload_data = $this->upload->data();
         $file_name = $upload_data['file_name'];

    }
    if($upload2 !== ""){

        $config['file_name']='upload2';
        $config['upload_path'] = './upload/';             
        $config['allowed_types'] = 'jpg|png|jpeg|gif';
        $config['max_size'] = '0'; // 0 = no file size limit           
        $config['overwrite'] = false;
        $this->load->library('upload', $config);
        $this->upload->do_upload('upload2');
        $upload_data = $this->upload->data();
        $file_name1 = $upload_data['file_name'];

    }
    if($upload3 !== ""){

        $config['file_name']='upload3';
        $config['upload_path'] ='./upload/';
        $config['allowed_types'] ='jpg|png|jpeg|gif';
        $config['max_size'] = '0';
        $config['overwrite'] = false;
        $this->load->library('upload',$config);
        $this->upload->do_upload('upload3');
        $upload_data = $this->upload->data();
        $file_name2 = $upload_data['file_name'];

    }
    if($upload4 !== ""){

        $config['file_name'] = 'upload4';
        $config['upload_path'] ='./upload/';
        $config['allowed_types'] = 'jpg|png|jpeg|gif';
        $config['max_size'] ='0';
        $config['overwrite'] = false;
        $this->load->library('upload',$config);
        $this->upload->do_upload('upload4');
        $upload_data = $this->upload->data();
        $file_name3 = $upload_data['file_name'];

    }
    if($upload5 !== ""){

        $config['file_name'] = 'upload5';
        $config['upload_path'] ='./upload/';
        $config['allowed_types'] = 'jpg|png|jpeg|gif';
        $config['max_size'] ='0';
        $config['overwrite'] = false;
        $this->load->library('upload',$config);
        $this->upload->do_upload('upload5');
        $upload_data = $this->upload->data();
        $file_name4 = $upload_data['file_name'];

    }
    if($upload6 !== ""){

        $config['file_name'] = 'upload6';
        $config['upload_path'] ='./upload/';
        $config['allowed_types'] = 'jpg|png|jpeg|gif';
        $config['max_size'] ='0';
        $config['overwrite'] = false;
        $this->load->library('upload',$config);
        $this->upload->do_upload('upload6');
        $upload_data = $this->upload->data();
        $file_name5 = $upload_data['file_name'];

    }
    if($upload7 !== ""){

        $config['file_name'] = 'upload7';
        $config['upload_path'] ='./upload/';
        $config['allowed_types'] = 'jpg|png|jpeg|gif';
        $config['max_size'] ='0';
        $config['overwrite'] = false;
        $this->load->library('upload',$config);
        $this->upload->do_upload('upload7');
        $upload_data = $this->upload->data();
        $file_name6 = $upload_data['file_name'];

    }
    if($upload8 !== ""){

        $config['file_name'] = 'upload8';
        $config['upload_path'] ='./upload/';
        $config['allowed_types'] = 'jpg|png|jpeg|gif';
        $config['max_size'] ='0';
        $config['overwrite'] = false;
        $this->load->library('upload',$config);
        $this->upload->do_upload('upload8');
        $upload_data = $this->upload->data();
        $file_name7 = $upload_data['file_name'];

    }



    $this->form_validation->set_rules('name','','xss_clean');
    $this->form_validation->set_rules('occasion','','xss_clean');
    $this->form_validation->set_rules('party_date','','xss_clean');
    $this->form_validation->set_rules('location','','xss_clean');
    $this->form_validation->set_rules('upload1','','xss_clean');
    $this->form_validation->set_rules('upload2','','xss_clean');
    $this->form_validation->set_rules('upload3','','xss_clean');
    $this->form_validation->set_rules('upload4','','xss_clean');
    $this->form_validation->set_rules('upload5','','xss_clean');
    $this->form_validation->set_rules('upload6','','xss_clean');
    $this->form_validation->set_rules('upload7','','xss_clean');
    $this->form_validation->set_rules('upload8','','xss_clean');
        $this->form_validation->run();

            $template_name = $this->input->post('template_name');
            $name = $this->input->post('name');
            $occasion = $this->input->post('occasion');
            $party_date = $this->input->post('party_date');
            $location = $this->input->post('location');
            $created = date('h:m:s a m/d/y');
            $profile_id = $this->input->post('profile_id');
            $this->admin_model->dashboard_templatedefault($template_name,$name,$occasion,$party_date,$location,$created,$profile_id,$upload1,$upload2,$upload3,$upload4,$upload5,$upload6,$upload7,$upload8);



}

1 Comment

problem for uploading image using 8 input files, base on the example above but you can use for loop if you want like for($i = 1; $i <=8; $1++){if(!empty('$upload'.$i)){ $config['file_name']='upload'.$i; $this->load->library('upload', $config); $this->upload->do_upload('upload'.$i); $upload_data = $this->upload->data(); $file_name = $upload_data['file_name']; }}
0

You code is seems too long try this one

 public function insertDepartmentImage()
    {
         $upload1 = $_FILES['upload1']['name'];      
         $upload2 = $_FILES['upload2']['name'];

        $config = array(
                'upload_path' => './uploads/galleryImg/',
                'log_threshold' => 1,
                'allowed_types' => 'jpg|png|jpeg|gif',
                'max_size' => '0',
                'overwrite' => false
            );
    for($i = 1; $i<=2; $i++){
        if(!empty('$upload'.$i)){
                        $config['file_name']='upload'.$i;
                        $this->load->library('upload', $config);
                        $this->upload->do_upload('upload'.$i);
                        $upload_data = $this->upload->data();
                        $file_name = $upload_data['file_name'];
                    }
    }
        $deptId = $this->input->post('deptId'); 
        $deptDetails = $this->input->post('deptDetails');
        $deptDetails1 = $this->input->post('deptDetails1');

        $this->Gallery_Model->insertDepartmentImage($deptId,$file_name,$file_name1,$deptDetails,$deptDetails1);



    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.