2017-01-27 54 views
0

我想更新我的數據在mysql的結構 | nip |納馬| path1 | path2 |代碼點火器不能上載文件

我想上傳的文件名 這是我的視圖代碼來改變

<form class="form-horizontal" method="post" action="<?php echo base_url()?>index.php/c_main/uploadFile" enctype="multipart/form-data"> 
    <div class="form-group"> 
    <label for="exampleInputFile">Berkas 1</label> 
      <input type="hidden" name="nip" id="nipUpload" value=""> 
      <input type="file" class="filestyle" name="berkas[]" data-buttonText="Find file" id="berkas[]"> 
      <p class="help-block">Example block-level help text here.</p> 
    </div> 
    <div class="form-group"> 
      <label for="exampleInputFile">Berkas 2</label> 
      <input type="file" class="filestyle" name="berkas[]" data-buttonText="Find file" id="berkas[]"> 
      <p class="help-block">Example block-level help text here.</p> 
    </div> 

      <div class="row"> 
       <button type="submit" class="btn btn-default">Upload File</button> 
      </div> 
</form> 

this is my controller code 

function uploadFile() 
     { 
      $nip = $_POST['nip']; 

      $number_of_files = sizeof($_FILES['berkas']['tmp_name']); 

      $cek = 0; 
      for($i=0;$i<$number_of_files;$i++) 
      { 
       $namaBerkas[$i] = $nip."-".($i+1).".jpg"; 

       $config['upload_path']   = base_url().'assets/upload/'; 
       $config['allowed_types']  = 'gif|jpg|png'; 
       $config['max_size']    = 1000; 
       $config['max_width']   = 2024; 
       $config['max_height']   = 1200; 
       $config['file_name']   = $namaBerkas; 

       $this->load->library('upload', $config); 
       // $this->upload->initialize($config); 

       if ($this->upload->do_upload()){ 
        $cek = $cek +1; 
       } 
       print_r($config); 
       echo $cek; 
       die(); 

      } 

      if($cek == 2){ 
       if($this->m_main->uploadFile($nip,$namaBerkas[0],[1])){ 
        echo "<script> alert ('Upload Berkas Berhasi!');</script>"; 
        redirect('admin/C_main/beranda'); 
       } 
      } 

     } 

這是我的模型

function upload_file($nip, $berkas1, $berkas2){ 
      $sql = "update daftar set PATH1 = $berkas1, PATH2 = $berkas2 WHERE NIP = $nip"; 
      return $this->db2->query($sql); 
     } 

上面的代碼的輸出是:你沒有選擇要上傳的文件。

+0

初始化上傳。刪除// $ $ this-> upload-> initialize($ config); – Ritchie

+0

已經嘗試過,但仍然得到了相同的輸出結果「您沒有選擇要上傳的文件」。 @Ritchie –

+0

查詢打印什麼? –

回答

0

我認爲問題是你有兩個相同的表單域。試試這個

<form class="form-horizontal" method="post" action="<?php echo base_url()?>index.php/c_main/uploadFile" enctype="multipart/form-data"> 
<div class="form-group"> 
<label for="exampleInputFile">Berkas 1</label> 
<input type="hidden" name="nip" id="nipUpload" value=""> 
<input type="file" class="filestyle" name="berkas[]" data-buttonText="Find file" id="berkas[]"> 
<p class="help-block">Example block-level help text here.</p> 
</div> 
<div class="row"> 
<button type="submit" class="btn btn-default">Upload File</button> 
</div> 
</form> 
+0

我想上傳2個文件並插入表 中的字段,其中表結構 | NIP | NAME | PATH1 | PATH2 | –

+0

選擇多個文件,循環瀏覽。使用$ _FILES [「berkas」] ['name']獲取文件名和pathinfo($ _ FILES [「berkas」] ['name'],PATHINFO_EXTENSION);得到文件擴展名。這應該工作。移除對$ this-> upload-> initialize($ config)的評論; – Ritchie

0
<form class="form-horizontal" method="post" action="<?php echo base_url()?>index.php/c_main/uploadFile" enctype="multipart/form-data"> 
    <div class="form-group"> 
    <label for="exampleInputFile">Berkas 1</label> 
      <input type="hidden" name="nip" id="nipUpload" value=""> 
      <input type="file" class="filestyle" name="berkas" data-buttonText="Find file" id="berkas"> 
      <p class="help-block">Example block-level help text here.</p> 
    </div> 
    <div class="form-group"> 
      <label for="exampleInputFile">Berkas 2</label> 
      <input type="file" class="filestyle" name="berkas1" data-buttonText="Find file" id="berkas1"> 
      <p class="help-block">Example block-level help text here.</p> 
    </div> 

      <div class="row"> 
       <button type="submit" class="btn btn-default">Upload File</button> 
      </div> 
</form> 

Conrtoller

function uploadFile() 
     { 
      $nip = $_POST['nip']; 

      $number_of_files = sizeof($_FILES['berkas']['tmp_name']); 

      $cek = 0;     
      $namaBerkas = $nip."-".1.".jpg"; 

       $config['upload_path']   = base_url().'assets/upload/'; 
       $config['allowed_types']  = 'gif|jpg|png'; 
       $config['max_size']    = 1000; 
       $config['max_width']   = 2024; 
       $config['max_height']   = 1200; 

       $config['file_name']   = $namaBerkas; 

       $this->load->library('upload', $config); 
       // $this->upload->initialize($config); 

       if ($this->upload->do_upload('berkas')){ 
        $cek = $cek +1; 
       } 

       $namaBerkas1 = $nip."-".2.".jpg"; 
       $config['file_name'] = $namaBerkas1; 
       if ($this->upload->do_upload('berkas1')){ 
        $cek = $cek +1; 
       } 

       print_r($config); 
       echo $cek; 
       die(); 



      if($cek == 2){ 
       if($this->m_main->uploadFile($nip,$namaBerkas[0],[1])){ 
        echo "<script> alert ('Upload Berkas Berhasi!');</script>"; 
        redirect('admin/C_main/beranda'); 
       } 
      } 

     } 
+0

感謝您的回答,但它仍然無法正常工作, 我試圖打印$ cek,但其值仍然爲0. 我認爲問題出在這段代碼「$ this-> upload-> doUpload()」中,因爲value $ cek沒有改變,但我不知道代碼有什麼問題。 –

+0

您是否將$ this-> upload-> doUpload()更改爲$ this-> upload-> doUpload('berkas')? –