codeigniter更新图片上传

问题描述 投票:0回答:2

嗨,我正在开发一个图像上传模块,其中图像路径保存在数据库中并在预览中检索,这是我的问题我希望它编辑和更新,但我的问题是它不会删除旧图像但会保存并更新新图像。

我希望你能帮助或建议我一个很好的教程,基本添加,编辑更新删除图像上传,其中将图像保存在数据库中。谢谢!

这是我在配置文件控制器中的编辑

    <?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">');?>
codeigniter image-processing upload
2个回答
0
投票

当你想覆盖旧的头像文件时,你应该从数据库加载旧图像的路径,

unlink
(删除)旧文件,然后写入新文件,最后将新文件的路径写入数据库.


0
投票

戴夫:

查看模型,其中:

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;
}

这可能是错误

© www.soinside.com 2019 - 2024. All rights reserved.