0

Codeigniter: Le fichier a été chargé avec succès lors de la définition d'un seul champ (individuel). Mais lors de la définition des deux champs deuxième fichier est également téléchargé à l'emplacement de la première. Comment résoudre ce conflit. Existe-t-il un didacticiel pour les zones de téléchargement multiples?Conflit lors du chargement d'images avec plusieurs champs de fichier dans le codeigniter

Mon code est donné ci-dessous:

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

. // codes de téléchargement d'images

  $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(); 
      } 

Répondre

0

Je pense que vous devriez faire une seule fonction pour télécharger des images qui a tableau de données en tant que paramètre où vous pouvez inclure le chemin et d'autres 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); 
    } 
    ... 
} 

Lorsque vous soumettez le formulaire, vérifiez tous les champs d'entrée avec le $ _FILES global et appeler la fonction de téléchargement si nécessaire avec le réseau de téléchargement ensemble de données $ pour chacun d'eux.