您好我正在開發一個圖片上傳模塊,其中的圖像路徑保存在數據庫中,檢索也在預覽和這裏是我的問題,我希望它編輯和更新,但我的問題是它不會刪除舊圖像,但它保存並更新新圖像。codeigniter更新圖片上傳
我希望你能幫忙或者建議我一個很好的教程基本添加,編輯更新刪除圖像上傳在哪裏在圖像保存在數據庫。謝謝!
這裏跟我的個人資料控制器
<?php
function edit_profile()
{
$id=$this->uri->segment(3);
$this->load->model('profile_model');
$this->load->library('form_validation');
// field name, error message, validation rules
$this->form_validation->set_rules('name', 'Name', 'trim|required');
$this->form_validation->set_rules('city', 'City', 'trim|required');
$this->form_validation->set_rules('telno', 'Tel no.', 'trim|required');
if ($this->form_validation->run() == FALSE)
{
if(empty($id)) $id=$this->input->post('id');
$data['result'] = $this->profile_model->profile_hotel($id);
$data['id']=$id;
$this->load->view('profile/edit_form', $data);
}
else{
if(isset($_FILES['profile_avatar']))
{
$this->load->model('profile_model');
$file = read_file($_FILES['upload']['tmp_name']);
$profile_avatar = basename($_FILES['upload']['name']);
write_file('files/'.$profile_avatar, $file);
$this->profile_model->update_profile($profile_avatar);
}
}
}
/* My update function in profile_model */
function update_profile($profile_field)
{
if(!empty($profile_field)){
$profile_avatar = $this->input->post('profile_avatar');
}
else{
$profile_avatar = $profile_field;
}
$id = $this->input->post('id');
$new_profile_update_data = array(
'name' => $this->input->post('name'),
'city' => $this->input->post('city'),
'telno' => $this->input->post('telno'),
'avatar' => $profile_avatar,
);
$this->db->where('id', $id);
$this->db->update('tbl_profile', $new_profile_update_data);
}
?>
/*My profile edit view*/
<?php
echo form_open_multipart('profile/edit_profile').'<br/>';
$fetch=$result->row();
?>
<img width="200" height="200" src="<?php echo base_url()?>files/<?php echo $fetch->hotel_logo;?>"><br/>
<?php
echo form_hidden('id',$id);
echo form_hidden('profile_avatar', $fetch->profile_avatar).'<br/>';
echo '<span>avatar</span>';
echo '<input type="file" name="profile_avatar" />'.'<br/>';
echo form_input('name', $fetch->name).form_error('name').'<br/>';
echo form_input('city', $fetch->city) .form_error ('city').'<br/>';
echo form_input('telno', $fetch->telno).form_error ('telno').'<br/>';
echo form_submit('submit', 'Save');
?>
<?php echo validation_errors('<p class="errors">');?>
無碼,沒有回答!展示下! – 2011-03-04 07:49:44
@Carlos Mora我已經添加了我的代碼我希望你能幫助我,謝謝! – Dave 2011-03-04 09:34:05