Postingan lainnya
Gagal update gambar pada php
saya masih pemula sehingga tidak mengerti mengapa gambar yang saya update tidak tampil / not found. berikut saya lampirkan controller serta viewnya..
Controller:
function petugas_update(){
$id_petugas = $this->input->post('id_petugas');
$nama = $this->input->post('nama');
$username = $this->input->post('username');
$password = $this->input->post('password');
$config['upload_path'] = './assets/upload/';
$config['allowed_types'] = 'jpg|png|jpeg';
$config['max_size'] = '2048';
$config['max_width'] = '1400';
$config['max_height'] = '1400';
$config['file_name'] = 'gambar'.time();
$this->load->library('upload',$config);
$where = array('id_petugas' => $id_petugas);
$data = array(
'nama' => $nama,
'username' => $username,
'gambar' => $image['file_name'],
'password' => md5($password)
);
if ($this->upload->do_upload('foto')){
$image = $this->upload->data();
unlink('assets/upload/'.$this->input->post('old_pict',TRUE));
$data['gambar'] = $image['file_name'];
$this->m_data->update_data($where,$data,'petugas');
} else{
$this->m_data->update_data($where,$data,'petugas');
}
$this->m_data->update_data($where,$data,'petugas');
redirect(base_url().'admin/petugas');
}
View:
<div class="container">
<div class="card">
<div class="card-header text-center">
<h4><i class="fa fa-street-view"></i> Edit Librarian Details</h4>
</div>
<div class="card-body">
<a href="<?php echo base_url().'admin/petugas' ?>" class='btn btn-sm btn-light btn-outline-dark pull-right'><i class="fa fa-arrow-left"></i> Back</a>
<br/>
<br/>
<?php foreach($petugas as $p){ ?>
<form method="post" action="<?php echo base_url().'admin/petugas_update'; ?>">
<div class="form-group">
<label class="font-weight-bold" for="nama">Full Name</label>
<input type="hidden" value="<?php echo $p->id_petugas; ?>" name="id_petugas">
<input type="text" class="form-control" name="nama" placeholder="Masukkan nama lengkap" required="required" value="<?php echo $p->nama; ?>">
</div>
<div class="form-group">
<label class="font-weight-bold" for="username">Username</label>
<input type="text" class="form-control" name="username" placeholder="Masukkan username" required="required" value="<?php echo $p->username; ?>">
</div>
<div class="form-group">
<label class="font-weight-bold">Gambar</label><br />
<?php
if(isset($p->gambar)){
echo '<input type="hidden" name="old_pict" value="'.$p->gambar.'">';
echo '<img src="'.base_url().'assets/upload/'.$p->gambar.'" width="20%">';
}
?>
<input name="foto" type="file" class="form-control">
</div>
<div class="form-group">
<label class="font-weight-bold" for="password">New Password</label>
<input type="password" class="form-control" name="password" placeholder="Type New Password">
</div>
<input type="submit" class="btn btn-primary" value="Save">
</form>
<?php } ?>
</div>
</div>
</div>
0
1 Jawaban:
Jawaban Terpilih
bukannya di tag pembuka <form> mesti ada tambah atribut enctype="multipart/form-data" agar bisa memproses file (seperti gambar)
Itu mungkin kurangnya sehingga error.
wallahu alam..
0