繁体   English   中英

如何在Codeigniter中上传图片

[英]How to upload image in 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(); ?>

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

使用codeigniter的文件助手

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

您的参考链接在这里

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

型号变更

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

}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM