简体   繁体   中英

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[]"

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

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