在Codeigniter 3中上载时如何显示不允许的文件类型错误消息?

问题描述 投票:1回答:1

我正在使用Codeigniter 3.1.8Bootstrap 4使用基本的[blog application

当然,这些帖子有主要图片。如果用户未上传图像,则为

默认图像,但如果图像is上传,则为仅允许3种类型:jpg,jpeg和png。

想要警告用户,如果他/他尝试上传其他文件格式,我在

Posts

控制器中执行了此操作:// Upload image $config['upload_path'] = './assets/img/posts'; $config['allowed_types'] = 'jpg|jpeg|png'; $config['max_size'] = '2048'; $this->load->library('upload', $config); if(!$this->upload->do_upload()){ $data['uerrors'] = $this->upload->display_errors(); if ($data['uerrors']) { $this->load->view('partials/header', $data); $this->load->view('dashboard/create-post'); $this->load->view('partials/footer'); } else { $post_image = 'default.jpg'; } } else { $data = array('upload_data' => $this->upload->data()); $post_image = $_FILES['userfile']['name']; }
在我看来:

<?php foreach ($uerrors as $uerror): ?> <span><?php echo $uerror; ?> </span> <?php endforeach; ?>

但是,我得到一个

未定义变量:uerrors错误。

这里是整个create()方法:

public function create() { // Only logged in users can create posts if (!$this->session->userdata('is_logged_in')) { redirect('login'); } $data = $this->get_data(); $data['tagline'] = "Add New Post"; if ($data['categories']) { foreach ($data['categories'] as &$category) { $category->posts_count = $this->Posts_model->count_posts_in_category($category->id); } } $this->form_validation->set_rules('title', 'Title', 'required'); $this->form_validation->set_rules('desc', 'Short description', 'required'); $this->form_validation->set_rules('body', 'Body', 'required'); $this->form_validation->set_error_delimiters('<p class="error-message">', '</p>'); if($this->form_validation->run() === FALSE){ $this->load->view('partials/header', $data); $this->load->view('dashboard/create-post'); $this->load->view('partials/footer'); } else { // Create slug (from title) $slug = url_title(convert_accented_characters($this->input->post('title')), 'dash', TRUE); $slugcount = $this->Posts_model->slug_count($slug, null); if ($slugcount > 0) { $slug = $slug."-".$slugcount; } // Upload image $config['upload_path'] = './assets/img/posts'; $config['allowed_types'] = 'jpg|jpeg|png'; $config['max_size'] = '2048'; $this->load->library('upload', $config); if(!$this->upload->do_upload()){ $data['uerrors'] = $this->upload->display_errors(); if ($data['uerrors']) { $this->load->view('partials/header', $data); $this->load->view('dashboard/create-post'); $this->load->view('partials/footer'); } else { $post_image = 'default.jpg'; } } else { $data = array('upload_data' => $this->upload->data()); $post_image = $_FILES['userfile']['name']; } $this->Posts_model->create_post($post_image, $slug); $this->session->set_flashdata('post_created', 'Your post has been created'); redirect('/'); } }

我的错误在哪里?
php validation codeigniter-3
1个回答
-1
投票
此错误告诉您变量不存在或未初始化。看这段代码

$data['uerrors'] = $this->upload->display_errors(); if ($data['uerrors']) {

我认为您可能在某个未初始化的地方(此代码未显示)中有一个$uerrors变量。请注意,我不认为array索引'uerrors'会在上面的块中给您带来麻烦,因为首先定义它,然后,如果您引用一个不存在的array项,那么您从问题中引用的错误消息中将得到不同的错误消息。
© www.soinside.com 2019 - 2024. All rights reserved.