我有一个奇怪的问题,我正在使用codeigniter上传图像,但事情很奇怪。 它保存到数据库,但其中一部分可以上传到特定文件夹,而另一个则不能。有人可以帮助我吗?我正在使用 Codeigniter 3 和 PHP 7.3.13
这是我的控制器:
public function edit_pegawai($id_pegawai){
if (isset($_POST['simpan'])){
$this->form_validation->set_rules('nama', 'Nama', 'required');
$data_pegawai=array(
'nik'=> $this->input->post('nik'),
'nama'=> $this->input->post('nama')
);
$data['dataPegawai'] = $data_pegawai;
if ($this->form_validation->run() == TRUE){
$img = $this->UserModel->get_foto($id_pegawai)->row();
$image = $img->foto;
if ($_FILES['foto']['name']){
if ($image != NULL){
unlink('./assets/img/foto/'.$image);
}
$nama = ucwords(strtolower($this->input->post('nama')));
$config['upload_path'] = './assets/img/foto/';
$config['allowed_types'] = 'jpe|jpg|jpeg';
$config['max_size'] = 10000;
$config['file_name'] = $nama;
$config['overwrite'] = TRUE;
$this->load->library('upload');
$this->upload->initialize($config);
if ($_FILES['foto']['type'] == 'image/jpeg' || $_FILES['foto']['type'] == 'image/png' || $_FILES['foto']['type'] != 'image/jpg'){
$this->upload->do_upload('foto');
$hilihh = $this->upload->data();
$foto = $hilihh['file_name'];
}
else{
$this->session->set_flashdata('msg_gagal');
redirect(site_url('listpegawai'));
}
$this->db->update('pns_local', $data_pegawai, array('id_pegawai'=>$id_pegawai));
$this->db->set('foto', $foto);
$this->db->where('id_pegawai', $id_pegawai);
$this->db->update('pns_local');
$this->session->set_flashdata('msg_berhasil');
redirect(site_url('listpegawai'));
}
这是我的观点
<form method="POST" action="<?= site_url('listpegawai/edit_pegawai/'.$dataPegawai['id_pegawai'])?>" enctype="multipart/form-data">
<input hidden type="text" name="id_pegawai" value="<?php echo $dataPegawai['id_pegawai']; ?>">
<div class="card bg-light">
<div class="form-row">
<div class="form-group col-md-4">
<label>NIK </label>
<input type="text" class="form-control" name="nik" value="<?php echo $dataPegawai['nik'];?>">
</div>
<div class="form-group col-md-4">
<label>Nama Lengkap </label>
<input type="text" class="form-control" name="nama" value="<?php echo $dataPegawai['nama'];?>">
</div>
<div class="form-row">
<div class="form-group col-md-10">
<label>Foto <span style="color: red">*</span></label><br>
<?php
if ($dataPegawai['foto'] != NULL){
?>
<img src="<?php echo base_url('assets/img/foto/'.$dataPegawai['foto'])?>" width="85" height="100">
<?php }
else{
?>
<img src="<?php echo base_url('assets/img/foto/default.png')?>" width="85" height="100">
<?php }
?>
<span> <input type="file" name="foto" accept="image/*"></span>
</div>
</div>
<button type="submit" name="simpan" class="btn btn-primary btn-block">Simpan Data
</button>
</div>
</form>
感谢您的帮助
设置 $config['log_threshold'] = 4;并在 application/logs/中查看 CI 日志
Php 日志也可以提供帮助。
在您的控制器中,尝试从上传路径中删除 ./