2016-08-09 44 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

相关问题