如何在codeigniter中上传文件

时间:2015-02-17 17:20:07

标签: mysql codeigniter upload

你好我试着在数据库中插入文件但是没有用 我上传的文件在数据库中的coloumn lampiran中给出值0 而不是存档在文件夹中来存储我的上传

数据库

 id|npp|tgl_pengajuan|ket|status|lampiran|lama|tgl_mulai|tgl_selesai |  id_jenis 

控制器

class Cutidiluar extends CI_Controller {

var $limit=10;
var $offset=10; 
 var $gallery_path;
var $gallery_path_url;
public function __construct(){
    parent::__construct();
    $this->gallery_path = realpath(APPPATH . '../asset');
    $this->gallery_path_url = base_url().'asset/';
} public function add()
        { if($this->session->userdata('LOGIN')=='TRUE')
     {
$this->load->library('form_validation');
$this->load->model('Diluartahunan_Model');
    //load uploading file library
    $config['upload_path'] = './asset/';
    $config['allowed_types'] = 'jpg|png|pdf|jpeg|word';

    $this->load->library('upload',$config);
    $this->upload->do_upload();
    $this->upload->data();

 $this->form_validation->set_rules('ket', 'ket');
 $this->form_validation->set_rules('lama', 'lama');
 $this->form_validation->set_rules('lampiran', 'lampiran');

if ($this->form_validation->run() == false) { 

 $data['nama']   = $this->Diluartahunan_Model->nama();
        $data['view'] = 'Cutidiluar/add';
         $data['judul']='';
        $this->load->view('index',$data);
        }else {

$this->load->model('Cutidiluar_Model');
$this->Cutidiluar_Model->add();
redirect('Cutidiluar');  
} }}

模型

public function add() {

$npp = $this->session->userdata('NPP');
$id_jenis = $this->input->post('id_jenis');
$ket = $this->input->post('ket');
$lama = $this->input->post('lama');
$tgl_selesai= date('Y-m-d',strtotime($this->input->post('tgl_selesai')));
$tgl_mulai= date('Y-m-d', strtotime($this->input->post('tgl_mulai')));
$lampiran = $this->input->post('lampiran');

$data = array(
'npp'=> $npp,
'id_jenis'=> $id_jenis,
'tgl_pengajuan' => date('y-m-d'),
'ket' => $ket,
'status' => 'P', 
'lampiran'=> $lampiran,
'lama'=> $lama,
'tgl_mulai'=> $tgl_mulai,
'tgl_selesai'=> $tgl_selesai,);
 $this->db->insert('Cuti_diluar', $data);}

视图

       <link rel="stylesheet" href="<?php echo base_url();?>css/themes/jquery.ui.all.css" type="text/css" />
  <script>
 $(document).ready(function() {
  $( ".datepicker" ).datepicker();
  });


  function save(){
  $.ajax({ 
  url:'<?php echo base_url(); ?>Cutidiluar/add/',     
  type:'POST',
  data:$('#frmsave').serialize(),
  success:function(data){ 
      if(data!=''){
       $( "#infodlg" ).html(data);
       $( "#infodlg" ).dialog({ title:"Info...", draggable:   false});           
    } else {
       window.location="<?php echo base_url() ?>Cutidiluar";
    } } }); }
       function confirmdlg(){
        $("#confirm").dialog({
       resizable: false,
       modal: true,
       title:"Info...",
       draggable: false,
       width: 'auto',

       height: 'auto',
       buttons: {
       "Ya": function(){
         save();   
          $(this).dialog("close");
          window.location="<?php echo base_url() ?>Cutidiluar";
        },
        "Tutup": function(){
           $(this).dialog("close");
        }
       }
      });

  }

  </script>


   <div class="span6">
                    <div class="well grey">
                        <div class="well-header">
                            <h5>Tambah Cuti  </h5>
                        </div>
            <div class="well-content no-search">
                            <form id="frmsave" name="frmsave"  class="form-validate" >


     <form id="frmsave" name="frmsave"  class="form-validate">


                    <h3>Detail </h3>



               <div class="form_row">

                                    <label class="field_name">Pilih cuti</label>
                                    <div class="field">
                                    <?php foreach ($nama->result() as $valnama) { ?>
                                       <input type="radio"  name="id_jenis"  value="<?php echo $valnama->id_jenis; ?>"> <?php echo $valnama->nama ?></br></br> <?php } ?>
                </div>
               </div>
                <div class="form_row">
                                    <label class="field_name">Tanggal Pengambilan Cuti</label>
                                    <div class="field">
                                         <input  placeholder="TANGGAL MULAI CUTI" class="datepicker" size="16" type="text" name="tgl_mulai" id="tgl_mulai" value="<?php echo set_value('tgl_mulai'); ?>" >
                   <input placeholder="TANGGAL SELESAI CUTI"  class="datepicker" size="16" type="text" name="tgl_selesai" id="tgl_selesai" value="<?php echo set_value('tgl_selesai'); ?>" >
                </div>
                                </div>
                 <div class="form_row">
                                    <label class="field_name">lama</label>
                                    <div class="field">
                                       <input type="text"  name="lama"  class="input-large"   value="<?php echo set_value('lama'); ?>" placeholder="masukan lama cuti"> 
                </div>
               </div>
                <div class="form_row">
                                    <label class="field_name">Lampiran</label>
                                    <div class="field">
                                       <input type="file" name="userfile">
                </div>
               </div>

               <div class="form_row">
                                    <label class="field_name">Keterangan (MAKS 50 KARAKTER)</label>
                                    <div class="field">
                                        <textarea  placeholder="KETERANGAN CUTI" id="ket" name="ket" class="span12" cols="40" rows="5" value="<?php echo set_value('ket'); ?>"></textarea>
                                    </div>
                                </div>


               <div class="form_row">
                                    <div class="field">
                                        <a onclick="return confirmdlg()" class="blue btn">Submit</a>
                                        <a  href="<?php echo base_url() ?>cuti_mpwp" class="red btn">Cancel</a>
                                    </div>
                                </div>

                                 </form>

我改变了我的观点和模型  在视野中

 <form id="frmsave" name="frmsave"  class="form-validate" enctype="multipart/form-data" >

这是我的模特

  public function add() {
 $npp = $this->session->userdata('NPP');
 $id_jenis = $this->input->post('id_jenis');
 $ket = $this->input->post('ket');
 $lama = $this->input->post('lama');
$tgl_selesai= date('Y-m-d', strtotime($this->input->post('tgl_selesai')));
$tgl_mulai= date('Y-m-d', strtotime($this->input->post('tgl_mulai')));
$lampiran = $_FILES['userfile']['name'];
$data = array(
'npp'=> $npp,
'id_jenis'=> $id_jenis,
'tgl_pengajuan' => date('y-m-d'),
'ket' => $ket,
'status' => 'P', 
'lampiran'=> $lampiran,
'lama'=> $lama,
'tgl_mulai'=> $tgl_mulai,
'tgl_selesai'=> $tgl_selesai);

并且结果无法插入我的数据库

1 个答案:

答案 0 :(得分:0)

上传过程必须在验证后完成,并使用$this->upload->display_errors()查看错误:

$this->form_validation->run() === TRUE) {
    $this->load->library('upload');
    $this->upload->initialize($config);

    if (!$this->upload->do_upload()){
        $data['errors'] =  $this->upload->display_errors(); // <-- HERE
        $this->load->view('index', $data);
    }
    else{
        $upload_data = $this->upload->data();

        $this->load->model('Cutidiluar_Model');
        $this->Cutidiluar_Model->add();

}