简体   繁体   中英

Conflict when uploading images with multiple file field in codeigniter

Codeigniter: File successfully upload when setting only one field(individual). But when setting both field second file is also uploaded to first's location. How to solve this conflict. Is there any tutorial for multiple upload field????

My code is given below:

<input type="file" name="filePrdimage" id="filePrdimage" size="20"/>
<input type="file" name="filePrdlogo" id="filePrdlogo" size="20"/>

. // Image uploading codes

          $config['upload_path'] = 'assets/images/b2bproduct';
            $config['allowed_types'] = 'gif|jpg|jpeg|png';
            $config['max_size'] = '1000';
            $config['max_width'] = '2024';
            $config['max_height'] = '1768';
            $config['overwrite'] = TRUE;
            $config['remove_spaces'] = TRUE;
            if (isset($_FILES['filePrdimage']['name'])) {
                $config['file_name'] = substr(md5(time()), 0, 28) . $_FILES['filePrdimage']['name'];
            }
            $this->load->library('upload', $config);
            if (!$this->upload->do_upload('filePrdimage')) {
                //no file uploaded or failed upload
                $error = array('error' => $this->upload->display_errors());
            } else {
                $dat = array('upload_data' => $this->upload->data());
                $this->resize($dat['upload_data']['full_path'], 'assets/images/b2bproduct/thump/'.$dat['upload_data']['file_name'],180,400);
            }

            if (empty($dat['upload_data']['file_name'])) {
               $prdimage = $this->input->post('hdPrdimage');            
            }
          else {

              $prdimage = $dat['upload_data']['file_name']; 
          }


//            End Image uploading Codes

 //           Logo uploading codes
            $config['upload_path'] = 'assets/images/b2blogo';
            $config['allowed_types'] = 'gif|jpg|jpeg|png';
            $config['max_size'] = '1000';
            $config['max_width'] = '2024';
            $config['max_height'] = '1768';
            $config['overwrite'] = TRUE;
            $config['remove_spaces'] = TRUE;
            if (isset($_FILES['filePrdlogo']['name'])) {
                $config['file_name'] = substr(md5(time()), 0, 28) . $_FILES['filePrdlogo']['name'];
            }
            $this->load->library('upload', $config);
            if (!$this->upload->do_upload('filePrdlogo')) {
                //no file uploaded or failed upload
                $error = array('error' => $this->upload->display_errors());
            } else {
                $dat = array('upload_data' => $this->upload->data());
                $this->resize($dat['upload_data']['full_path'], 'assets/images/b2blogo/'.$dat['upload_data']['file_name'],135,300);

            }
            if (empty($dat['upload_data']['file_name'])) {
               $prdlogo= $this->input->post('hdPrdlogo'); ;            
            }
          else {
              $prdlogo=$dat['upload_data']['file_name']; 
          }          
//            End Logo uploading Codes 

  public function resize($source,$destination,$width,$height) {
                $config['image_library'] = 'gd2';
                $config['source_image'] = $source;
                $config['create_thumb'] = FALSE;
                $config['maintain_ratio'] = TRUE;
                $config['width'] = $width;
                $config['height'] = $height;
                $config['new_image'] = $destination;
                $this->load->library('image_lib', $config);
                $this->image_lib->resize();
            }

I think you should do only one function to upload images that has data array as parameter where you may include the path and other variables.

public function upload(array $data)
{
   $config['upload_path'] = isset($data['upload_path']) ? $data['upload_path'] : 'default/path';
   ...
   $this->load->library('upload', $config);       

   if (!$this->upload->do_upload($data['field')) {
      //no file uploaded or failed upload
      $error = array('error' => $this->upload->display_errors());
   } else {
      $dat = array('upload_data' => $this->upload->data());
      $this->resize($dat['upload_data']['full_path'], $data['upload_path'].$dat['upload_data']['file_name'],135,300);
     }
     ...
}

When you submit the form, check for all input fields with the $_FILES global and call the upload function when necessary with the upload array $data set for each of them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM