jeudi 21 mai 2020

how to upload three different image files in three different folders in codeigniter

I am new to codeigniter. I have a form in which there are 3 file inputs. User can upload one or two or all three files. I have written a code which works fine if any of one file is uploaded at a time. If two/three uploaded then gives error. I have searched then it says i have to use $this->upload->initialize($config2); for second upload if first file is uploaded but in my case there is possibility that first is not uploaded or file upload depends on user choice. How should I need to handle this and their errors? Please help and guide. Thanks in advance.

My code is as follows:

 // load base_url  
             $this->load->helper('url');

             // Check form submit or not 
             if($this->input->post('upload') != NULL ){ 
               // $data = array(); 
               if(!empty($_FILES['logoFile']['name'])){ 
                // Set preference 
                $config['upload_path'] = 'uploads/logo/'; 
                $config['allowed_types'] = 'jpg|jpeg|png'; 
                $config['max_size'] = '100'; // max_size in kb 
                $config['file_name'] = $_FILES['logoFile']['name']; 


                // Load upload library 
                $this->load->library('upload',$config); 
                $this->upload->overwrite = true;

                            // File upload
                            if($this->upload->do_upload('logoFile')){ 
                             // Get data about the file
                             $uploadData = $this->upload->data(); 
                             $filename = $uploadData['file_name']; 
                             $filepath = $uploadData['full_path'];


                                $logo_data = array('logo_upload_data' => $this->upload->data());

$imagedata = array(

        'file_name' =>   $filename, //insert your image name from here..
        );

        $this->SettingsModel->logo_image_upload($imagedata); //that will go to the model function..

                                 $this->session->set_userdata('imagesrc', 'uploads/logo/'.$filename);

                                $this->load->view('private/upload_success', $logo_data);




                            }else{ 
                              $error = array('error' => $this->upload->display_errors());

                              $this->load->view('private/ThemeSettings', $error);
                            } 
               }
               // else{ 
               //  $error = array('error' => 'filename empty');
               //  $this->load->view('private/ThemeSettings', $error);
               // } 


if(!empty($_FILES['bgImageFile']['name'])){ 
                // Set preference 
                $config1['upload_path'] = 'uploads/bgimage/'; 
                $config1['allowed_types'] = 'jpg|jpeg|png'; 
                $config1['max_size'] = '500'; // max_size in kb 
                $config1['file_name'] = $_FILES['bgImageFile']['name']; 


                // Load upload library 
                $this->load->library('upload',$config1); 
                $this->upload->overwrite = true;

                            // File upload
                            if($this->upload->do_upload('bgImageFile')){ 
                             // Get data about the file
                             $uploadData1 = $this->upload->data(); 
                             $filename1 = $uploadData1['file_name']; 
                             $filepath1 = $uploadData1['full_path'];


                                $bgimage_data = array('bgimage_upload_data' => $this->upload->data());

$imagedata1 = array(

        'file_name' =>   $filename1, //insert your image name from here..
        );

        $this->SettingsModel->bgimage_image_upload($imagedata1); //that will go to the model function..

                                 $this->session->set_userdata('bgimagesrc', 'uploads/bgimage/'.$filename1);

                                $this->load->view('private/upload_success', $bgimage_data);




                            }else{ 
                              $error = array('error' => $this->upload->display_errors());

                              $this->load->view('private/ThemeSettings', $error);
                            } 
               }
               // else{ 
               //  $error = array('error' => 'filename empty');
               //  $this->load->view('private/ThemeSettings', $error);
               // } 


if(!empty($_FILES['bgImageAllFile']['name'])){ 
                // Set preference 
                $config2['upload_path'] = 'uploads/bgimageall/'; 
                $config2['allowed_types'] = 'jpg|jpeg|png'; 
                $config2['max_size'] = '500'; // max_size in kb 
                $config2['file_name'] = $_FILES['bgImageAllFile']['name']; 


                // Load upload library 
                $this->load->library('upload',$config2); 
                $this->upload->overwrite = true;

                            // File upload
                            if($this->upload->do_upload('bgImageAllFile')){ 
                             // Get data about the file
                             $uploadData2 = $this->upload->data(); 
                             $filename2 = $uploadData2['file_name']; 
                             $filepath2 = $uploadData2['full_path'];


                                $bgimageall_data = array('bgimageall_upload_data' => $this->upload->data());

$imagedata2 = array(

        'file_name' =>   $filename2, //insert your image name from here..
        );

        $this->SettingsModel->bgimage_image_upload($imagedata2); //that will go to the model function..

                                 $this->session->set_userdata('bgimagesrc', 'uploads/bgimageall/'.$filename2);

                                $this->load->view('private/upload_success', $bgimageall_data);




                            }else{ 
                              $error = array('error' => $this->upload->display_errors());

                              $this->load->view('private/ThemeSettings', $error);
                            } 
               }



              }else{
               // load view 
                $error = array('error' => 'form is not submitted');
               $this->load->view('private/ThemeSettings', $error); 
              } 

Aucun commentaire:

Enregistrer un commentaire