dongmaqiu6084 2016-09-23 08:35
浏览 41

如何在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.

  • 写回答

4条回答 默认 最新

  • dongtuo2373 2016-09-23 08:43
    关注

    use file helper of codeigniter

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

    reference link for you is here

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向