我正在使用Codeigniter 3.1.8和Bootstrap 4开发一个基本的博客应用程序。
当然,这些帖子有主要图像。如果用户没有上传图像,则有默认图像,但如果上传图像,则只允许 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');
}
我的错误在哪里?
慕桂英4014372
jeck猫
慕码人2483693