Hi Master,

I cant unlink or update my uploaded image. I can successfully upload image bt i cant use unlink function or Any body help me how can i remove / Update uploaded image.

Here is my Controller:

public function addProfileLogo() {
        $data = array();
        $errors = '';
        if ($_FILES['logo_image']['name'] && $_FILES['logo_image']['size']) {
            $result = $this->logoUpload('logo_image');

// here logo_image = table culm name.



        if ($result) {
            if ($result['file_name']) {
                $data['logo_image'] = $result['file_name'];
                if ($data['logo_image']->logo_image)
                  {
                  unlink($data['logo_image']->logo_image);
                  } 
                  $data['user_id'] = $this->session->userdata('user_id');
                 $this->user_admin_model->saveProfilelogo($data);  // saveProfilelogo = model fun name.
            } else {
                $errors = $result['error'];
            }
        }
    }

    if (($errors!="") ) {
        $err['exception'] = "You do Upload correct Formet!";
        $this->session->set_userdata($err);          

    }
    redirect("user_admin_controller/editProfileLogo", 'refresh');
}

public function logoUpload($fieldName) {
    $config['upload_path'] = 'image/instlogos/';
    $config['allowed_types'] = 'gif|jpg|png|jpeg';
    $config['overwrite'] = FALSE;
    $config['max_size'] = '200';
    $config['max_width'] = '';
    $config['max_height'] = '';
    $this->load->library('Upload');
    $this->upload->initialize($config);
    if ($this->upload->do_upload($fieldName)) {
        $data = $this->upload->data();
        $fileName = $config['upload_path'] . $data['file_name'];
        $return = array('file_name' => $fileName, 'error' => '');
        return $return;
    } else {
        $err = '';
        echo $this->upload->display_errors();
        $return = array('file_name' => '', 'error' => $err);
        return $return;
    }
}

My Model is:

public function saveProfilelogo($data) {

        $this->db->insert('tbl_profile_logo', $data);
        return $this->db->affected_rows();
    }

Image Edit/Remove Form View:

<form action="<?php echo base_url(); ?>user_admin_controller/addProfileLogo" enctype="multipart/form-data" method="post">
                <input type="file" name="logo_image" required="1" accept="image/png, image/jpeg, image/jpg" class="span5"/>
                <input type="submit" value="Edit Logo"/>
</form>

please help me..Thanks

The problem in your script seems related with this $data['logo_image']->logo_image, the script tries to get an object from an array, you should get an error for this.

Anyway change this block:

if ($result) {
    if ($result['file_name']) {
        $data['logo_image'] = $result['file_name'];
        if ($data['logo_image']->logo_image)
          {
          unlink($data['logo_image']->logo_image);
          } 
          $data['user_id'] = $this->session->userdata('user_id');
         $this->user_admin_model->saveProfilelogo($data);  // saveProfilelogo = model fun name.
    } else {
        $errors = $result['error'];
    }
}

With:

if( ! empty($result['file_name']))
{
    if(file_exists($result['file_name']))
    {
        log_message('debug', 'file '. $result['file_name'] .' exists.');
        unlink($result['file_name']);
        log_message('debug', 'file '. $result['file_name'] .' deleted.');

        $data['user_id'] = $this->session->userdata('user_id');
        $data['logo_image'] = $result['file_name'];
        $this->user_admin_model->saveProfilelogo($data);
    }
    else
    {
        log_message('debug', 'file '. $result['file_name'] .' not found.');
        $errors = 'File '.$result['file_name'].' not found.';
    }
}
else
{
    log_message('debug', $result['error']);
    $errors = $result['error'];
}

It should work. Be sure to enable CodeIgniter logs, in order to see errors and other debugging information, it will be useful.

Reference: http://ellislab.com/codeigniter/user-guide/general/errors.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.