首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用辅助器显示来自do_upload的错误消息

如何使用辅助器显示来自do_upload的错误消息
EN

Stack Overflow用户
提问于 2016-09-23 13:08:19
回答 1查看 914关注 0票数 0

我创建图像模块,编辑超过1mb的图像,这样就不能显示错误信息。我用的是副显贵的零工。

控制器:

代码语言:javascript
复制
   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 (!empty($_FILES['agent_image']['name'])) {
                if ($this->upload->do_upload('agent_image')) {
                    $_POST['agent_img_url'] = 'uploads/agent/' . $this->upload->data('file_name');
                } else {
                    $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);
                }
            }
            $this->agent_model->update($_POST, $id);
            alert('Update', $_POST['agent_name']);
            redirect('agent');
        }
    }

模型:

代码语言:javascript
复制
  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);
    }

视图:

代码语言:javascript
复制
 <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>

我的问题是:我为特定用户编辑图像然后上传图像,但如果图像大小大于1mb,则图像无法上传并显示错误信息。所以我的问题是如何显示错误。

EN

回答 1

Stack Overflow用户

发布于 2016-09-23 13:13:50

代码语言:javascript
复制
$uploaded = $this->upload->do_upload('file'); //'file' is input field name

if($uploaded) {
    $upload_data = $this->upload->data();
    // do database stuff
} else {
    $data['errors'] = array("error" => $this->upload->display_errors());
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39661603

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档