如何在codeigniter上传图像

时间:2016-09-23 08:35:13

标签: php codeigniter

控制器:

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

型号:

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

查看:

<?= 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(); ?>

您好我正在开发图像上传模块和图像路径保存在数据库中并进行检索。 我的问题我希望它能够编辑和更新但我的问题是它不会删除文件夹中的旧图像,但会保存并更新新图像。

4 个答案:

答案 0 :(得分:1)

使用codeigniter的文件助手

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

您的参考链接是here

答案 1 :(得分:1)

使用保存在数据库中的文件名删除,使用PHP unlink(../filename.jpg)并从文件中删除

答案 2 :(得分:0)

模型改变

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

}

答案 3 :(得分:-1)

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