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

我正在使用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');

    } 

    

我的错误在哪里?


拉丁的传说
浏览 95回答 3
3回答

慕桂英4014372

通过修改这种方式,我已成功显示上传错误(如果尝试上传,否则使用默认图像):create()public function create() {&nbsp; &nbsp; // Only logged in users can create posts&nbsp; &nbsp; if (!$this->session->userdata('is_logged_in')) {&nbsp; &nbsp; &nbsp; &nbsp; redirect('login');&nbsp; &nbsp; }&nbsp; &nbsp; $data = $this->get_data();&nbsp; &nbsp; $data['tagline'] = "Add New Post";&nbsp; &nbsp; if ($data['categories']) {&nbsp; &nbsp; &nbsp; &nbsp; foreach ($data['categories'] as &$category) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $category->posts_count = $this->Posts_model->count_posts_in_category($category->id);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; $this->form_validation->set_rules('title', 'Title', 'required');&nbsp; &nbsp; $this->form_validation->set_rules('desc', 'Short description', 'required');&nbsp; &nbsp; $this->form_validation->set_rules('body', 'Body', 'required');&nbsp; &nbsp; $this->form_validation->set_error_delimiters('<p class="error-message">', '</p>');&nbsp; &nbsp; if($this->form_validation->run() === FALSE){&nbsp; &nbsp; &nbsp; &nbsp; $this->load->view('partials/header', $data);&nbsp; &nbsp; &nbsp; &nbsp; $this->load->view('dashboard/create-post');&nbsp; &nbsp; &nbsp; &nbsp; $this->load->view('partials/footer');&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; // Create slug (from title)&nbsp; &nbsp; &nbsp; &nbsp; $slug = url_title(convert_accented_characters($this->input->post('title')), 'dash', TRUE);&nbsp; &nbsp; &nbsp; &nbsp; $slugcount = $this->Posts_model->slug_count($slug, null);&nbsp; &nbsp; &nbsp; &nbsp; if ($slugcount > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $slug = $slug."-".$slugcount;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // Upload image&nbsp; &nbsp; &nbsp; &nbsp; $config['upload_path'] = './assets/img/posts';&nbsp; &nbsp; &nbsp; &nbsp; $config['allowed_types'] = 'jpg|jpeg|png';&nbsp; &nbsp; &nbsp; &nbsp; $config['max_size'] = '2048';&nbsp; &nbsp; &nbsp; &nbsp; $this->load->library('upload', $config);&nbsp; &nbsp; &nbsp; &nbsp; if(!$this->upload->do_upload()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $errors = array('error' => $this->upload->display_errors());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Display upload validation errors&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // only if a file is uploaded and there are errors&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (empty($_FILES['userfile']['name'])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $errors = [];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (empty($errors)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $post_image = 'default.jpg';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $data['upload_errors'] = $errors;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $data = array('upload_data' => $this->upload->data());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $post_image = $_FILES['userfile']['name'];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (empty($errors)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->Posts_model->create_post($post_image, $slug);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->session->set_flashdata('post_created', 'Your post has been created');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; redirect('/');&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->load->view('partials/header', $data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->load->view('dashboard/create-post');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->load->view('partials/footer');&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}在create-post.php我看来:<?php if(isset($upload_errors)){&nbsp; &nbsp;foreach ($upload_errors as $upload_error) {&nbsp; &nbsp; echo $upload_error;&nbsp; &nbsp;}&nbsp;}?>

jeck猫

我在这里捡到了三样东西1) 如前所述,没有将 $data 传递给正确的视图2) 在视图上期望数组而不是字符串,即错误的数据类型3) 最后函数 do_upload() 需要参数字符串 $field。缺少这就是为什么您只有 no upload selected 错误。如果设置了这个参数,codeigniter 真的会抛出错误的文件类型错误。我这样做是为了测试在我看来<form action="http://localhost:8000/welcome/create" method="post" enctype="multipart/form-data">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="file" name="lname" ><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="submit" value="Submit">&nbsp; &nbsp; &nbsp; &nbsp; </form>然后在我的控制器中if(!$this->upload->do_upload("lname")){上传错误的文件类型以测试此错误。您可能需要额外的长度来检测实际上传文件的文件类型。

慕码人2483693

您的上传代码看起来不错,但您需要更新以下这些更改。将数据传递给您的'dashboard/create-post'视图,就像您传递给您的'partials/header'视图一样。您的'dashboard/create-post'视图没有收到任何上传错误消息,所以它说'Undefined variable: uerrors'. 所以,你的上传代码应该是这样的 -if(!$this->upload->do_upload()){&nbsp; &nbsp; $data['uerrors'] = $this->upload->display_errors();&nbsp; &nbsp; if ($data['uerrors']) {&nbsp; &nbsp; &nbsp; &nbsp; $this->load->view('partials/header', $data);&nbsp; &nbsp; &nbsp; &nbsp; $this->load->view('dashboard/create-post', $data);&nbsp; &nbsp; &nbsp; &nbsp; $this->load->view('partials/footer');&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; $post_image = 'default.jpg';&nbsp; &nbsp; }} else {&nbsp; &nbsp; $post_image = $this->upload->data('file_name');}正如CodeIgniter 文档所说,'display_errors()' 返回字符串,而不是数组,您不必遍历错误。只是在你的'dashboard/create-post'观点上呼应它。为了您的方便,请以不同的方法制作您的上传任务,以便您也可以在更新方法中重新使用它。例如——private function uploadFile(){&nbsp; &nbsp; if ($_FILES['userfile']['name'] === '') {&nbsp; &nbsp; &nbsp; &nbsp; return array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'status' => TRUE,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'message' => 'No file selected.',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'file_name' => 'default.jpg'&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; }&nbsp; &nbsp; // Upload image&nbsp; &nbsp; $config['upload_path'] = './assets/img/posts';&nbsp; &nbsp; $config['allowed_types'] = 'jpg|jpeg|png';&nbsp; &nbsp; $config['max_size'] = '2048';&nbsp; &nbsp; $this->load->library('upload', $config);&nbsp; &nbsp; if(!$this->upload->do_upload('userfile')){&nbsp; &nbsp; &nbsp; &nbsp; return array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'status' => FALSE,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'message' => $this->upload->display_errors('<p class="text-danger ">', '</p>'),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'file_name' => ''&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; return array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'status' => TRUE,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'message' => 'File uploaded successfully',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'file_name' => $this->upload->data('file_name')&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; }}然后你的整个创建方法应该是这样的 -public function create() {&nbsp; &nbsp; // Only logged in users can create posts&nbsp; &nbsp; if (!$this->session->userdata('is_logged_in')) {&nbsp; &nbsp; &nbsp; &nbsp; redirect('login');&nbsp; &nbsp; }&nbsp; &nbsp; $data = $this->get_data();&nbsp; &nbsp; $data['tagline'] = "Add New Post";&nbsp; &nbsp; if ($data['categories']) {&nbsp; &nbsp; &nbsp; &nbsp; foreach ($data['categories'] as &$category) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $category->posts_count = $this->Posts_model->count_posts_in_category($category->id);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; $this->form_validation->set_rules('title', 'Title', 'required');&nbsp; &nbsp; $this->form_validation->set_rules('desc', 'Short description', 'required');&nbsp; &nbsp; $this->form_validation->set_rules('body', 'Body', 'required');&nbsp; &nbsp; $this->form_validation->set_error_delimiters('<p class="error-message">', '</p>');&nbsp; &nbsp; if($this->form_validation->run() === FALSE){&nbsp; &nbsp; &nbsp; &nbsp; $this->load->view('partials/header', $data);&nbsp; &nbsp; &nbsp; &nbsp; $this->load->view('dashboard/create-post');&nbsp; &nbsp; &nbsp; &nbsp; $this->load->view('partials/footer');&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; $upload = $this->uploadFile();&nbsp; &nbsp; &nbsp; &nbsp; if($upload['status'] === FALSE){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $data['upload_error'] = $upload['message'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->load->view('partials/header', $data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->load->view('dashboard/create-post', $data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->load->view('partials/footer');&nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Create slug (from title)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $slug = url_title(convert_accented_characters($this->input->post('title')), 'dash', TRUE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $slugcount = $this->Posts_model->slug_count($slug, null);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($slugcount > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $slug = $slug."-".$slugcount;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->Posts_model->create_post($upload['file_name'], $slug);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->session->set_flashdata('post_created', 'Your post has been created');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; redirect('/');&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}最后在您的视图文件中添加这行代码'dashboard/create-post',就在文件输入按钮之后。<?php if(isset($upload_error)) echo $upload_error; ?>我认为一切都应该工作。
打开App,查看更多内容
随时随地看视频慕课网APP