Postingan lainnya
Error undefined variable upload file dengan php framework codeigniter
Saya mau membuat form dengan upload file menggunakan php framework CI tetapi saya menemui error seperti ini :
A PHP Error was encountered Severity: Notice
Message: Undefined variable: file
Filename: controllers/Bukusk.php
Line Number: 182
Backtrace:
File: C:\xampp\htdocs\siagsurat\application\controllers\Bukusk.php Line: 182 Function: _error_handler
File: C:\xampp\htdocs\siagsurat\index.php Line: 315 Function: require_once
A Database Error Occurred Error Number: 1048
Column 'file' cannot be null
codingan saya seperti ini : 1. Controller Bukusk.php
$this->_rules();
if ($this->form_validation->run() == FALSE) {
$this->create();
}
else
{
$config['upload_path'] = './uploads/bukusk';
$config['allowed_types'] = 'pdf|jpg|jpeg|png';
$config['max_size'] = 2048;
$this->upload->initialize($config);
if(!$this->upload->do_upload())
{
$no = $this->Bukusk_model->total_data()+1;
$data = array(
'button' => 'Tambah',
'action' => site_url('bukusk/create_action'),
'navbar' => $this->navbar(),
'id_bukusk' => set_value('id_bukusk'),
'no' => set_value('no',$no),
'tgl_srt' => set_value('tgl_srt',date('d-m-Y')),
'no_sk1' => set_value('no_sk1',$no),
'no_sk2' => set_value('no_sk2'),
'no_sk3' => set_value('no_sk3',$this->romawi(date('m'))),
'no_sk4' => set_value('no_sk4',date('Y')),
'uraian' => set_value('uraian'),
'tahun' => set_value('tahun',date('Y')),
'menu' => 'Buku SK *Upload Error'.$this->input->post('berkas'),
'submenu' => 'Tambah Buku SK',
'backsub' => 'bukusk',
'lockmenu' => 'Tambah Buku SK',
'ui_sidebar_active' => 'Buku SK',
'isi' => 'bukusk/tb_bukusk_form',
);
$this->load->view('template/wrapper', $data);
}
else
{
$data_berkas = $this->upload->data();
$file = $data_berkas['file_name'];
}
$data = array(
'no' => $this->input->post('no',TRUE),
'tgl_srt' => date('Y/m/d',strtotime($this->input->post('tgl_srt',TRUE))),
'no_sk1' => $this->input->post('no_sk1',TRUE),
'no_sk2' => $this->input->post('no_sk2',TRUE),
'no_sk3' => $this->input->post('no_sk3',TRUE),
'no_sk4' => $this->input->post('no_sk4',TRUE),
'uraian' => $this->input->post('uraian',TRUE),
'tahun' => $this->input->post('tahun',TRUE),
'file' => $file,
'id_user' => $this->session->userdata('id_user'),
);
$this->Bukusk_model->insert($data);
$this->session->set_flashdata('message', 'Create Record Success');
redirect(site_url('bukusk'));
}
2. View Form Input
<form enctype="multipart/form-data" action="<?php echo $action; ?>" method="POST">
<div class="form-group">
<div class="row">
<label for="int" class="col-sm-2 control-label">No <?php echo form_error('no') ?></label>
<div class="col-sm-3">
<input type="text" class="form-control" name="no" id="no" placeholder="No" value="<?php echo $no; ?>" readonly/>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<label for="date" class="col-sm-2 control-label">Tanggal Surat <?php echo form_error('tgl_srt') ?></label>
<div class="col-sm-3">
<!--
<input type="date" class="form-control" name="tgl_srt" id="tgl_srt" placeholder="Tgl Srt" value="<?php echo date('d/m/Y',strtotime($tgl_srt)); ?>" id="tanggal"/>
-->
<div class="input-group date">
<div class="input-group-addon">
<i class="fa fa-calendar"></i>
</div>
<input type="text" name="tgl_srt" class="form-control pull-right" id="tgl_srt" value="<?php echo date('d-m-Y',strtotime($tgl_srt)); ?>">
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<label class="col-sm-2 control-label">Nomor SK <?php echo form_error('no_sk') ?></label>
<div class="col-sm-1">
<input type="text" class="form-control" name="no_sk1" id="no_sk1" placeholder="Nomor SK" value="<?php echo $no_sk1; ?>" readonly/>
</div>
<div class="col-sm-1">
<center>/</center>
</div>
<div class="col-sm-2">
<input type="text" class="form-control" name="no_sk2" id="no_sk2" placeholder="" value="<?php echo $no_sk2; ?>" />
</div>
<div class="col-sm-1">
<center>/</center>
</div>
<div class="col-sm-2">
<input type="text" class="form-control" name="no_sk3" id="no_sk3" placeholder="Nomor SK" value="<?php echo $no_sk3; ?>" readonly/>
</div>
<div class="col-sm-1">
<center>/</center>
</div>
<div class="col-sm-2">
<input type="text" class="form-control" name="no_sk4" id="no_sk4" placeholder="Nomor SK" value="<?php echo $no_sk4; ?>" readonly/>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<label for="varchar" class="col-sm-2 control-label">Uraian <?php echo form_error('uraian') ?></label>
<div class="col-sm-6">
<input type="text" class="form-control" name="uraian" id="uraian" placeholder="Uraian" value="<?php echo $uraian; ?>" />
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<label for="varchar" class="col-sm-2 control-label">Tahun <?php echo form_error('tahun') ?></label>
<div class="col-sm-3">
<input type="text" class="form-control" name="tahun" id="tahun" placeholder="" value="<?php echo $tahun; ?>" readonly/>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<label for="varchar" class="col-sm-2 control-label">File</label>
<div class="col-sm-3">
<input type="file" class="form-control-file" name="berkas" />
</div>
</div>
</div>
<input type="hidden" name="id_bukusk" value="<?php echo $id_bukusk; ?>" />
<button type="submit" class="btn btn-primary"><?php echo $button ?></button>
<a href="<?php echo site_url('bukusk') ?>" class="btn btn-default">Batal</a>
</form>
saya juga sudah load library dan helper nya di autoload.php
$autoload['libraries'] = array('table', 'form_validation', 'database', 'session','simple_login','upload');
$autoload['helper'] = array('form','html','url');
Mohon bantuannya untuk pencerahannya, para suhu programmer :)
1 Jawaban:
coba ganti ini <pre> //$this->upload->do_upload() //jadi ini $this->upload->do_upload('berkas') </pre>