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">');?>
    
已邀请:
当您想要覆盖旧的头像文件时,您应该从数据库加载旧图像的路径,
unlink
(删除)旧文件,然后编写新文件,最后将新文件的路径写入数据库。     
戴夫: 看看模型,其中:
if (!empty($profile_field)) {
    $profile_avatar = $this->input->post('profile_avatar');
} else {
    $profile_avatar = $profile_field;
}
我觉得有一个'!'那是错的。它应该是
if (empty($profile_field)) {
    $profile_avatar = $this->input->post('profile_avatar');
} else {
    $profile_avatar = $profile_field;
}
可能这是错误     

要回复问题请先登录注册