简体   繁体   English

在codeigniter中上传2个或更多具有不同字段名称和不同输入的图像

[英]upload 2 or more image with different field name and different input in codeigniter

i want to upload several image with different input and different field name for each image. 我想为每个图像上传具有不同输入和不同字段名称的多个图像。 i actually try to separate it in two different method like this 我实际上尝试以两种不同的方法将其分开

 public function tambahketua(){
     if(isset($_POST["reg"]))
        {

            $config['upload_path'] = './asset/img/foto/';
            $config['allowed_types'] = 'gif|jpg|png|jpeg|pdf|doc|xml|docx|GIF|JPG|PNG|JPEG|PDF|DOC|XML|DOCX|xls|xlsx';
            $config['max_size'] = 200048;
            $config['file_name'] = $this->input->post("namaketua");

            $this->upload->initialize($config);

            if ( ! $this->upload->do_upload('attachment'))//ini name di input choose file
            {

            }
            else
            {
            $datas = $this->upload->data();
            $edit['image_library'] = 'gd2';
            $edit['source_image']   = './asset/img/foto/'.$datas['file_name'];
            $edit['create_thumb'] = TRUE;
            $edit['maintain_ratio'] = TRUE;
            $edit['height'] = 600;
            $this->load->library('image_lib', $edit);
            $this->image_lib->resize();
            $edit_file = explode('.', $datas['file_name']);
            $data['foto'] = $edit_file[0].'_thumb.'.$edit_file[1];
            unlink($config['upload_path'].$datas['file_name']);
            }


            $a = $this->miniatur_model->insertPeserta($data);
            if($a){
                $this->tambahanggota1();
            }
            else{
                echo "failed";
            }

    }
        $this->load->view('pendaftaran');
}


 public function tambahanggota1(){
    if(isset($_POST["reg"]))
        {
            $config['upload_path'] = './asset/img/foto/';
            $config['allowed_types'] = 'gif|jpg|png|jpeg|pdf|doc|xml|docx|GIF|JPG|PNG|JPEG|PDF|DOC|XML|DOCX|xls|xlsx';
            $config['max_size'] = 200048;
            $config['file_name'] = $this->input->post("namaanggota1");
            $this->upload->initialize($config);

            if ( ! $this->upload->do_upload('fotoktmanggota1'))//ini name di input choose file
            {

            }
            else
            {
            $datas = $this->upload->data();
            $edit['image_library'] = 'gd2';
            $edit['source_image']   = './asset/img/foto/'.$datas['file_name'];
            $edit['create_thumb'] = TRUE;
            $edit['maintain_ratio'] = TRUE;
            $edit['height'] = 600;
            $this->load->library('image_lib', $edit);
            $this->image_lib->resize();
            $edit_file = explode('.', $datas['file_name']);
            $data['foto'] = $edit_file[0].'_thumb.'.$edit_file[1];
            unlink($config['upload_path'].$datas['file_name']);
            }

            $a = $this->miniatur_model->insertPeserta($data);
            if($a){
                echo "success";
            }
            else{
                echo "failed";
            }
    }        
    $this->load->view('pendaftaran');
}

and so far i could only write both of the image name into my database, but only the first one uploaded into my folder. 到目前为止,我只能将两个图像名称都写入数据库,但是只能将第一个图像名称写入文件夹。 and this is my view 这是我的看法

<input type="file" id="attachment" name="attachment"> <input type="file" id="fotoktmanggota1" name="fotoktmanggota1">

and for some reason i cant change the name of my files into array like name="attachment[]" 由于某种原因,我无法将文件名更改为name="attachment[]"类的数组

I have minimized your extra code. 我已将您的多余代码减至最少。 Try this and do your own magic if I missed something. 试试这个,如果我错过了什么,做自己的魔术。

function tambahketua() {
  if (isset($_POST["reg"])) {
    $config['upload_path'] = './asset/img/foto/';
    $config['allowed_types'] = 'gif|jpg|png|jpeg|pdf|doc|xml|docx|GIF|JPG|PNG|JPEG|PDF|DOC|XML|DOCX|xls|xlsx';
    $config['max_size'] = 200048;

    $this->upload->initialize($config);

    if (!$this->upload->do_upload('attachment')) {
      $error = array('error' => $this->upload->display_errors());
     // var_dump($error); 
    } else {
      $fileData = $this->upload->data();
      $data['foto'] = $fileData['file_name'];
      $this->doResize($fileData['file_name']);
    }
    $insertedFirstImage = $this->miniatur_model->insertPeserta($data);
    if ($insertedFirstImage) {
      // Upload Second Image on Success
      if (!$this->upload->do_upload('fotoktmanggota1')) {
        $error = array('error' => $this->upload->display_errors()); 
      } else {
        $fileData = $this->upload->data();
        $data['foto'] = $fileData['file_name'];
        $this->doResize($fileData['file_name']);
      }
      $insertedSecondImage = $this->miniatur_model->insertPeserta($data);
     /*---------------------------------------*/
   } else{
     echo "Image insertion Failed in database";
   }
 }
 $this->load->view('pendaftaran');
}

function doResize($filename) {
  $sourcePath = './asset/img/foto/' . $filename;
  $targetPath = './asset/img/foto/thumb/thumb_' . $filename;

  $config_manip = array(
      'image_library' => 'gd2',
      'source_image' => $sourcePath,
      'new_image' => $targetPath,
      'maintain_ratio' => true,
      'width' => 150,
      'height' => 150
  );
  $this->image_lib->initialize($config_manip);
  $this->load->library('image_lib', $config_manip);


 if (!$this->image_lib->resize()) {
   echo $this->image_lib->display_errors();
   exit;
 }
 // clear //
 // $this->image_lib->clear();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Codeigniter使用不同的字段名称上传多个图像 - Codeigniter multiple image upload with different field names 使用codeigniter上传多个不同名称的图像 - upload multiple image with different name using codeigniter 如何在Codeigniter中使用不同的输入文件上传多个图像 - how to upload multiple image with different input file in codeigniter CodeIgniter 3. 上传图片两次,但文件名不同 - CodeIgniter 3. Upload image twice but with different file name 在Codeigniter中,是否可能以单一形式具有多个输入/浏览文件以用于不同的图像上传,以将所有数据保存在1个表中的db中? - It is possible in codeigniter to have more than 1 input/browse file for different image upload in a single form to save all in 1 table in a db? Codeigniter上传具有数组字段名称的图像文件 - Codeigniter upload image file with array field name 在Codeigniter中使用不同的输入文件上传多个图像 - Uploading multiple image with different input file in Codeigniter CodeIgniter上传图像无输入 - CodeIgniter Upload Image no input 上传名称不同的图片,但保留文件扩展名 - Upload image with different name but keep file extension 与Codeigniter中的其他输入字段一起验证可选的图片上传字段 - Validate optional image upload field with other input fields in Codeigniter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM