2016-08-09 68 views
0

我有一個問題,上傳.pdf文件從模態到數據庫。該問題顯示錯誤消息「您沒有選擇要上傳的文件」。 請幫我解決我的問題Codeigniter PHP文件上傳從模式到數據庫的錯誤(您沒有選擇要上傳的文件。)

我的語氣

<?php echo form_open_multipart('pengaduan/simpanberkas');?> 
<form class="form-horizontal" action="" method="post" name="berkas" enctype="multipart/form-data" /> 
    <div class="form-group"> 
     <label class="col-lg-2 control-label">Berkas</label> 
     <div class="col-lg-5"> 
      <input type="file" name="berkas" class="berkas" id="berkas" > 
     </div> 
      <label>Format berkas .PDF, .doc, .docx, atau .xls</label> 
    </div> 
    <div class="controls"> 
     <a href="<?php echo base_url();?>index.php/pengaduan/simpanberkas/<?php echo $key;?>"><button type="submit" name="tambah" id="tambah" class="btn btn-primary btn-small" value="tambah">Upload</button></a>  
    </div> 
</form> 

我控制器

public function simpanberkas() 
    { 
     $key = $this->uri->segment(3); 
     $this->load->model('model_pengaduan'); 
     $query = $this->model_pengaduan->getdata($key); 
     if($query->num_rows>0) 
     { 
      foreach ($query->result() as $row) { 
       $data['file'] = $row->file; 
      } 
     } 

     $config['upload_path'] = './assets/file/'; 
     $config['allowed_types'] = 'pdf|doc|docx|excel'; 
     $config['max_size'] = '1000'; 
     //$config['max_width'] = '2000'; 
     //$config['max_height'] = '1024'; 
     $this->load->library('upload',$config); 
     //$this->upload->initialize($config); 
     //$berkas = $this->input->post('berkas'); 
      if(!$this->upload->do_upload('berkas')){ 
        $error = array('error' => $this->upload->display_errors()); 
        $this->session->set_flashdata('info',$error['error']); 
      }else{ 
        $file_data=$this->upload->data(); 
        $data['file']=base_url().'/assets/file/'.$file_data['file_name']; 
      } 

      $this->model_pengaduan->getupdate($key,$data); 
      redirect('pengaduan');  
    } 
+0

[這](HTTP://計算器。 com/questions/7457152/did-not-select-a-file-to-upload-when-uploadloading-using-codeigniter)可能會回答你的問題。 – csabinho

+0

我試過了,它不起作用 –

回答

0

檢查與螢火蟲,如果你的表單打開顯示在地址的IP,那麼你可能有麻煩提交表單。

確保您已設置自己的基本網址在config.php

/* 
|-------------------------------------------------------------------------- 
| Base Site URL 
|-------------------------------------------------------------------------- 
| 
| URL to your CodeIgniter root. Typically this will be your base URL, 
| WITH a trailing slash: 
| 
| http://example.com/ 
| 
| WARNING: You MUST set this value! 
| 
| If it is not set, then CodeIgniter will try guess the protocol and path 
| your installation, but due to security concerns the hostname will be set 
| to $_SERVER['SERVER_ADDR'] if available, or localhost otherwise. 
| The auto-detection mechanism exists only for convenience during 
| development and MUST NOT be used in production! 
| 
| If you need to allow multiple domains, remember that this file is still 
| a PHP script and you can easily do that on your own. 
| 
*/ 

$config['base_url'] = 'http://localhost/your_project/'; 

形式

<?php echo form_open_multipart('pengaduan/simpanberkas/' . $key);?> 

<!-- form content --> 

<button type="submit">Save</button> 

<?php echo form_close();?> 

這將在$鍵值發送到您的simpanberkas功能,您可能需要設置您的路線它

$route['pengaduan/simpanberkas'] = 'pengaduan/simpanberkas'; 
$route['pengaduan/simpanberkas/(:any)'] = 'pengaduan/simpanberkas/$1'; 

然後在控制器上得到密鑰使用uri段

控制器:您的文件和類名應該只有第一個字母 大寫只喜歡

文件名:Pengaduan.php

<?php 

class Pengaduan extends CI_Controller { 

    public function simpanberkas() { 

     $this->load->library('upload'); 

     $key = $this->uri->segment(3); 
     $this->load->model('model_pengaduan'); 
     $query = $this->model_pengaduan->getdata($key); 

     if($query->num_rows > 0) { 

     foreach ($query->result() as $row) { 
       $data['file'] = $row->file; 
      } 
     } 

     $config['upload_path'] = './assets/file/'; 
     $config['allowed_types'] = 'pdf|doc|docx|excel'; 
     $config['max_size'] = '1000'; 

     //$config['max_width'] = '2000'; 
     //$config['max_height'] = '1024'; 

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

     if(!$this->upload->do_upload('berkas')){ 

      $error = array('error' => $this->upload->display_errors()); 
      $this->session->set_flashdata('info',$error['error']); 

     } else { 

      // Success part redirects stuff here 

      $file_data=$this->upload->data(); 
      $data['file']=base_url().'/assets/file/'.$file_data['file_name']; 

      this->model_pengaduan->getupdate($key,$data); 
      redirect('pengaduan');  
     } 

    } 

} 
+0

該按鈕不起作用 –

+0

你的意思是不起作用你得到了什麼錯誤? – user4419336

+0

在config - > mimes.php中添加了''pdf'=> array('application/pdf'),' – user4419336

相關問題