在Codeigniter中上载图像时出错



我在codigniter中开发了项目,我认为代码是基于codeigniter文档的。当我上传图像时,它说"无法加载请求的文件",错误在控制器函数simpan((中,你能帮我解决这个问题吗?

模型中的此代码:

<?php class Mahasiswa_model extends CI_Model{
function simpan_mahasiswa($npm,$nama,$prodi,$fakultas,$angkatan,$email,$image_name,$image){ 
$data = array(
'npm'       => $npm,
'nama'      => $nama,
'prodi'     => $prodi, 
'fakultas'  => $fakultas,
'angkatan'  => $angkatan,
'email'     => $email,
'qr_code'   => $image_name,
'gambar'    => $image
);
$this->db->insert('mahasiswa',$data);
} }

控制器中的此代码:

<?php class Mahasiswa extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model('mahasiswa_model'); //pemanggilan model mahasiswa
}
function simpan(){
//membuat konfigurasi
$config['upload_path']="./assets/images";
$config['allowed_types']='gif|jpg|png';
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
if($this->upload->do_upload("userfile")){
$data = array('upload_data' => $this->upload->data());
$npm=$this->input->post('npm');
$nama=$this->input->post('nama');
$prodi=$this->input->post('prodi');
$fakultas=$this->input->post('fakultas');
$angkatan=$this->input->post('angkatan');
$email=$this->input->post('email');
$image= $data['upload_data']['file_name']; 
$this->mahasiswa_model->simpan_mahasiswa($npm,$nama,$prodi,$fakultas,$angkatan,$email,$image_name,$image); //simpan ke database
redirect('mahasiswa'); //redirect ke mahasiswa usai simpan data
}
else{
echo "error";
}
} }

此代码是模态形式,视图名称为"mahasiswa_view.php":

<!-- Modal add new mahasiswa-->
<form action="<?php echo base_url().'index.php/mahasiswa/simpan'?>" method="post">
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title" id="myModalLabel">Add New Mahasiswa</h4>
</div>
<div class="modal-body">
<div class="form-group">
<label for="npm" class="control-label">NPM:</label>
<input type="text" name="npm" class="form-control" id="npm" required> 
</div>
<div class="form-group">
<label for="nama" class="control-label">NAMA:</label>
<input type="text" name="nama" class="form-control" id="nama" required>
</div>
<div class="form-group">
<label for="prodi" class="control-label">PRODI:</label>
<input type="text" name="prodi" class="form-control" id="prodi" required>
</div>
<div class="form-group">
<label for="fakultas" class="control-label">FAKULTAS:</label>
<select name="fakultas" class="form-control" id="fakultas">
<option>FMIPA</option>
<option>FKG</option>
<option>FTIP</option>
<option>FH</option>
<option>FISIP</option>
</select>
</div>
<div class="form-group">
<label for="angkatan" class="control-label">ANGKATAN:</label>
<select name="angkatan" class="form-control" id="angkatan">
<option>2017</option>
<option>2016</option>
<option>2015</option>
<option>2014</option>
<option>2013</option>
<option>2012</option>
</select>
</div>
<div class="form-group">
<label for="email" class="control-label">EMAIL:</label>
<input type="email" name="email" class="form-control" id="email">
</div>
<div class="form-group">
<label for="userfile">Gambar :</label>
<input type="file" name="userfile" class="file">
<div class="input-group col-xs-12">
<span class="input-group-addon"><i class="glyphicon glyphicon-picture"></i></span>
<input type="text" class="form-control input-lg" disabled placeholder="Upload Gambar">
<span class="input-group-btn">
<button class="browse btn btn-primary input-lg" type="button"><i class="glyphicon glyphicon-search"></i> Telusuri</button>
</span>
</div><br>
</div>   

</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Tutup</button>
<button type="submit" name="submit" class="btn btn-primary" >Simpan</button>
</div>
</div>
</div>
</div>
</form>
<script type="text/javascript" src="<?php echo base_url().'assets/js/jquery-2.1.4.min.js'?>"></script>
<script type="text/javascript" src="<?php echo base_url().'assets/js/bootstrap.js'?>"></script>
<script type="text/javascript">
$(document).on('click', '.browse', function(){
var file = $(this).parent().parent().parent().find('.file');
file.trigger('click');
});
$(document).on('change', '.file', function(){
$(this).parent().find('.form-control').val($(this).val().replace(/C:\fakepath\/i, ''));
});
</script>

如果库有问题,请在调用do_upload函数后重试:

$this->upload->display_errors('<p>', '</p>');

此外,如果你正在上传.jpeg文件,你需要将.jpeg添加到你允许的类型中。

$config['allowed_types']='gif|jpg|png|jpeg';

相关内容

  • 没有找到相关文章

最新更新