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

How to display disallowed file type error message in case of upload in Codeigniter 3?

我正在使用 Codeigniter 3.1.8[=46 开发基本的 blog application =] 4.

帖子当然有主图。如果用户没有上传图片,则有一个默认图片,但如果上传图片,则只有3张允许的类型:jpg、jpeg 和 png。

想要在 she/he 尝试上传其他文件格式时警告用户,我在 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('/');
    }
} 

我的错误在哪里?

此错误告诉您变量不存在或未初始化。看这段代码

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

if ($data['uerrors']) {

我认为您可能在某处有一个未初始化的 $uerrors 变量(此代码中未显示)。请注意,我不相信 array 索引 'uerrors' 会给你在上面的块中带来麻烦,因为首先你定义它,其次,如果你引用一个 array 项目不存在,那么您将收到与问题中引用的错误消息不同的错误消息。

你的问题有点含糊。不管怎样,设置变量 $uerrors 的唯一条件是 create() 方法何时执行,我相信它会在 POST 请求时执行。另外,你没说这是哪部分的view:

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

如果是 dashboard/create-post 视图,则尝试将 $data 直接传递给此视图,而不是将其传递给 partials/header

注意: 我刚刚检查了 codeigniter View/Controller samples,我发现使用 isset() 函数调用检查变量是一个很好的做法,所以改为要直接执行 foreach 循环,请执行以下操作:

<? if (isset($uerrors)): ?>
    <? foreach ($uerrors as $uerror): ?>
      <span><?= $uerror; ?></span>
    <? endforeach; ?> 
<? endif; ?> 

您的上传代码看起来没问题,但您需要更新以下更改。

  1. 将数据传递给 'dashboard/create-post' 视图,就像传递给 'partials/header' 视图一样。您的 'dashboard/create-post' 视图没有收到任何上传错误消息,所以显示 'Undefined variable: uerrors'。所以,你的上传代码应该是这样的 -
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', $data);
        $this->load->view('partials/footer');
    } else {
        $post_image = 'default.jpg';
    }
} else {
    $post_image = $this->upload->data('file_name');
}
  1. 正如CodeIgniter Documentation所说,'display_errors()' returns 字符串,不是数组,你不必遍历错误。只需在您的 'dashboard/create-post' 视图中回显即可。

为了您的方便,请使用不同的方法进行上传任务,以便您也可以在更新方法中重新使用它。例如-

private function uploadFile(){
    if ($_FILES['userfile']['name'] === '') {
        return array(
            'status' => TRUE,
            'message' => 'No file selected.',
            'file_name' => 'default.jpg'
        );
    }

    // 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('userfile')){
        return array(
            'status' => FALSE,
            'message' => $this->upload->display_errors('<p class="text-danger ">', '</p>'),
            'file_name' => ''
        );
    }else{
        return array(
            'status' => TRUE,
            'message' => 'File uploaded successfully',
            'file_name' => $this->upload->data('file_name')
        );
    }
}

那么你的整个创建方法应该是这样的 -

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 {
        $upload = $this->uploadFile();

        if($upload['status'] === FALSE){
            $data['upload_error'] = $upload['message'];

            $this->load->view('partials/header', $data);
            $this->load->view('dashboard/create-post', $data);
            $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;
            }

            $this->Posts_model->create_post($upload['file_name'], $slug);
            $this->session->set_flashdata('post_created', 'Your post has been created');
            redirect('/');
        }
    }
}

最后将这行代码添加到您的 'dashboard/create-post' 查看文件中,就在文件输入按钮之后。

<?php if(isset($upload_error)) echo $upload_error; ?>

我认为一切都应该可行。

您需要将 $data['uerrors'] 初始化为 null 因为您在前端使用它

$data['uerrors'] = '';

或者在前端检查该值是否为空。

前端,你可以这样做:

<?php  if (isset($uerrors) && $uerrors != '') {
            foreach ($uerrors as $uerror) {
                echo '<span>' . $uerror . '</span>';
            }
        } ?>

您的控制器将是:

 $data = array();
        // Upload image
        $config['upload_path'] = './assets/img/posts';
        $config['allowed_types'] = 'jpg|jpeg|png';
        $config['max_size'] = '2048';

        $this->load->library('upload', $config);

        /*You need to initialize  $data['uerrors'] as null because you are using it on front end*/
        $data['uerrors'] = '';
        if (!$this->upload->do_upload('FilePath')) {
            $data['uerrors'] = $this->upload->display_errors();
            $this->load->view('partials/header', $data);
            $this->load->view('dashboard/create-post');
            $this->load->view('partials/footer');
        } else {
            if (isset($_POST{'your_file'})) {
                /*'check your images here that you are receiving from front end'*/
            } else {
                /*If there is no image, then default image is*/
                $post_image = 'default.jpg';
            }
            $data = array('upload_data' => $this->upload->data());
            $post_image = $_FILES['userfile']['name'];
        }

您可以在 How to upload image in CodeIgniter?

上进一步找到有用的帖子

我在这里捡到了三样东西

1) 没有像之前提到的那样将 $data 传递给正确的视图

2) 在视图中需要数组而不是字符串,即错误的数据类型

3) 最后函数 do_upload() 需要参数字符串 $field。缺少这就是为什么您只有 no upload selected 错误。如果设置了这个参数,codeigniter 真的会抛出错误的文件类型错误。我这样做是为了测试

在我看来

<form action="http://localhost:8000/welcome/create" method="post" enctype="multipart/form-data">
          <input type="file" name="lname" ><br>
          <input type="submit" value="Submit">
        </form>

然后在我的控制器中

if(!$this->upload->do_upload("lname")){

上传错误的文件类型以测试此错误。您可能需要花更多的时间来检测实际上传文件的文件类型。

我通过修改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()){

            $errors = array('error' => $this->upload->display_errors());

            // Display upload validation errors 
            // only if a file is uploaded and there are errors
            if (empty($_FILES['userfile']['name'])) {
                $errors = [];
            }

            if (empty($errors)) {
                $post_image = 'default.jpg';
            } else {
                $data['upload_errors'] = $errors;
            }

        } else {
            $data = array('upload_data' => $this->upload->data());
            $post_image = $_FILES['userfile']['name'];
        }

        if (empty($errors)) {
            $this->Posts_model->create_post($post_image, $slug);
            $this->session->set_flashdata('post_created', 'Your post has been created');
            redirect('/');
        } else {
            $this->load->view('partials/header', $data);
            $this->load->view('dashboard/create-post');
            $this->load->view('partials/footer');
        }
    }
}

create-post.php 视图中我有:

<?php if(isset($upload_errors)){
   foreach ($upload_errors as $upload_error) {
    echo $upload_error;
   }
 }?>