kishan kakadiya
kishan kakadiya

Reputation: 3

How to upload image in codeigniter

controller:

public function edit($id) {
    $this->edit_status_check($id);
    $this->form_validation->set_rules('agent_name', 'Agent Name', 'required');
    $this->form_validation->set_rules('mobile', 'Mobile No.', 'required');
    $this->form_validation->set_rules('agent_vehicle', 'Agent Vehicle', 'required');
    if ($this->form_validation->run() == FALSE) {
        $data = array(
            'page_title' => 'Edit Agent',
            'page_name' => 'agent/edit',
            'result' => $this->agent_model->select_id($id),
            'result_vehicle' => $this->vehicle_model->list_all(),
            'error' => validation_errors(),
            'id' => $id
        );
        $this->load->view('template', $data);
    } else {
        $config['upload_path'] = '../uploads/agent/';
        $config['allowed_types'] = 'jpg|jpeg';
        $config['encrypt_name'] = TRUE;
        $config['max_size'] = 1000; // 1 mb
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload('agent_image')) {
            $data = array(
                'page_title' => 'Edit Agent',
                'page_name' => 'agent/edit',
                'result' => $this->agent_model->select_id($id),
                'result_vehicle' => $this->vehicle_model->list_all(),
                'error' => $this->upload->display_errors(),
                'id' => $id
            );
            $this->load->view('template', $data);
        } else {
            $_POST['agent_img_url'] = 'uploads/agent/' . $this->upload->data('file_name');
            $this->agent_model->update($_POST, $id);
            alert('Update', $_POST['agent_name']);
            redirect('agent');
        }
    }
}

Model:

public function update($data, $id) {
    $updatedata = array(
        'name' => $data['agent_name'],
        'mobile' => $data['mobile'],
        'password' => sha1($data['password']),
        'vehicle' => $data['agent_vehicle'],
        'address' => $data['agent_address'],
        'category' => $data['category'],
        'created_on' => date('Y-m-d h:i:sa')
    );
    if (!empty($data['agent_img_url'])) {
        $updatedata['img_url'] = $data['agent_img_url'];
    }
    $this->db->where('id', $id);
    $this->db->update('agent', $updatedata);
}

View:

<?= form_open_multipart('agent/edit/' . $id); ?>
    <?php if (!empty($error)): ?>
        <div class="alert alert-danger alert-dismissible" role="alert">
            <?= $error; ?>
        </div>
    <?php endif; ?>
    <div class="form-group">
        <img src="/<?= $result['img_url']; ?>" class="img-responsive" name="old_agent_image" width="133" height="100">
    </div>
    <div class="form-group">
        <label>Agent Image</label>
        <input type="file" name="agent_image">
    </div>
    <button type="submit" class="btn btn-success">Update</button>
<?= form_close(); ?>

Hi I'm developing a image upload module and image path save in database and retrieve. my Question I want it to edit and update but the my problem is it doesn't delete the old image in folder, but it save and update the new image.

Upvotes: 0

Views: 102

Answers (4)

Vijay Makwana
Vijay Makwana

Reputation: 921

Change in Model

public function update($data, $id) {
$updatedata = array(
    'name' => $data['agent_name'],
    'mobile' => $data['mobile'],
    'password' => sha1($data['password']),
    'vehicle' => $data['agent_vehicle'],
    'address' => $data['agent_address'],
    'category' => $data['category'],
    'created_on' => date('Y-m-d h:i:sa')
);
if (!empty($data['agent_img_url'])) {

   $updatedata['agent_img_url'] = $data['agent_img_url'];
}

$q = $this->db->where('id',$id)
                  ->get('agent');
    $query = $q->row_array();
    @unlink("./asset/uploads/".$query['agent_img_url']);

$this->db->where('id', $id);
$this->db->update('agent', $updatedata);

}

Upvotes: 0

Pratap Tomar
Pratap Tomar

Reputation: 21

if (!$this->upload->do_upload($name)) { 
    $data = array('msg' => $this->upload->display_errors()); 
} else { 
    $data = array('msg' => "success"); 
    $databasea['upload_data'] = $this->upload->data(); 
    $this->load->library('image_lib'); 
    return $databasea['upload_data']['file_name']; 
} 
return ''; 

Upvotes: -1

Bira
Bira

Reputation: 5506

Delete using the file name saved in the database, use the PHP unlink(../filename.jpg) and delete from files

Upvotes: 1

Rohit Yadav
Rohit Yadav

Reputation: 47

use file helper of codeigniter

$this->load->helper("file");
delete_files($path);

reference link for you is here

Upvotes: 1

Related Questions