簡體   English   中英

在Codeigniter中上傳具有多個文件字段的圖像時發生沖突

[英]Conflict when uploading images with multiple file field in codeigniter

Codeigniter:僅設置一個字段(單個)時文件成功上傳。 但是同時設置兩個字段時,第二個文件也將上傳到第一個文件的位置。 如何解決這個沖突。 是否有關於多個上傳字段的教​​程????

我的代碼如下:

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

//圖片上傳代碼

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

我認為您應該只執行一項功能,以上傳具有數據數組作為參數的圖像,其中可以包含路徑和其他變量。

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);
     }
     ...
}

提交表單時,請檢查所有帶有$ _FILES全局變量的輸入字段,並在必要時調用上載函數,並為每個輸入字段設置上載數組$ data。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM