Dave
Dave

Reputation: 1

codeigniter update image upload

Hi I'm developing a image upload module where the image path save in database and retrieve also in preview and here is my question I want it to edit and update but the my problem is it doesn't delete the old image but it save and update the new image.

I hope you can help or suggest me a good tutorial basic add, edit-update delete image upload where in it save the image in DB. Thanks!

Here with is my edit in profile controller

    <?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">');?>

Upvotes: 0

Views: 8680

Answers (2)

Carlos Mora
Carlos Mora

Reputation: 1184

Dave:

look in the model, where :

if (!empty($profile_field)) {
    $profile_avatar = $this->input->post('profile_avatar');
} else {
    $profile_avatar = $profile_field;
}

i think there is a '!' that is wrong. it should be

if (empty($profile_field)) {
    $profile_avatar = $this->input->post('profile_avatar');
} else {
    $profile_avatar = $profile_field;
}

may be this is the mistake

Upvotes: 0

janosrusiczki
janosrusiczki

Reputation: 1931

When you want to overwrite an old avatar file you should load the path to your old image from the database, unlink (delete) the old file, then write the new file and lastly write the path to the new file to the database.

Upvotes: 0

Related Questions