图像上传的错误消息在codeigniter中显示太多

时间:2018-10-16 07:08:36

标签: php codeigniter

现在我正在使用CodeIgniter框架进行php项目。现在我正在创建注册表格,需要4种类型的输入文件来上传图像。

这是我的查看代码:

Upload image 1 :
<input type="file" class="images" name="image1">
<?php echo form_error('image1'); ?> 

Upload image 2 :
<input type="file" class="images" name="image2">
<?php echo form_error('image2'); ?> 

这是我的控制器

public function create()
{
    $post = $this->input->post();

    $res = $this->_register_validate();

    if(!$res){
        $this->add();
    }else{
        var_dump("success validation");
        exit;
    }
}

    private function _register_validate()
    { 

        if(!empty($_FILES['image1']['name']))
        {
            $this->form_validation->set_rules('image1','Upload image 1','callback_check_images1_upload');
        }

    if(!empty($_FILES['image2']['name']))
        {
            $this->form_validation->set_rules('image2','Upload image 2','callback_check_images2_upload');
        }


        $this->form_validation->set_error_delimiters('<span class="error message">', '</span>');

        if ($this->form_validation->run() == FALSE)
    {
        $res = false;
    }else{
        $res = true;
    }
    return $res;
    }

function check_images1_upload()
        {
        $config['upload_path'] = './public/admin/uploads/';
            $config['allowed_types'] = 'gif|jpg|png|jpeg';
            $config['max_size']   = '10';
            $config['max_width']  = '3000';
            $config['max_height'] = '3000';

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

            if ( ! $this->upload->do_upload('images1'))
            {
                    $this->form_validation->set_message('check_images1_upload', $data['error'] = $this->upload->display_errors());
            return false;
            }
            else
            {
                    return true;
            }
        }

    function check_images2_upload()
        {
        $config['upload_path'] = './public/admin/uploads/';
            $config['allowed_types'] = 'gif|jpg|png|jpeg';
            $config['max_size']   = '10';
            $config['max_width']  = '3000';
            $config['max_height'] = '3000';

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

            if ( ! $this->upload->do_upload('images2'))
            {
                    $this->form_validation->set_message('check_images2_upload', $data['error'] = $this->upload->display_errors());
            return false;
            }
            else
            {
                    return true;
            }
        } 

但是显示错误“ images2”的结果变成这样的两倍

enter image description here

如果我想显示“ images2”中的错误消息,该怎么办?

1 个答案:

答案 0 :(得分:0)

从set_messsage中删除$data['error']

  public function create()
    {
       $post = $this->input->post();

       $res = $this->_register_validate();

       if(!$res){
           $this->add();
       }else{
           var_dump("success validation");
        exit;
       }
    }

    private function _register_validate()
    { 
        if(!empty($_FILES['image1']['name']))
        {
            $this->form_validation->set_rules('image1','Upload image 
                         1','callback_check_images1_upload');
        }

    if(!empty($_FILES['image2']['name']))
        {
            $this->form_validation->set_rules('image2','Upload image 2','callback_check_images2_upload');
        }


        $this->form_validation->set_error_delimiters('<span class="error message">', '</span>');

        if ($this->form_validation->run() == FALSE)
    {
        $res = false;
    }else{
        $res = true;
    }
    return $res;
    }

function check_images1_upload()
        {
        $config['upload_path'] = './public/admin/uploads/';
            $config['allowed_types'] = 'gif|jpg|png|jpeg';
            $config['max_size']   = '10';
            $config['max_width']  = '3000';
            $config['max_height'] = '3000';

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

            if ( ! $this->upload->do_upload('images1'))
            {
                    $this->set_message('check_images1_upload', $this->upload- 
                    >display_errors());
            return false;
            }
            else
            {
                    return true;
            }
        }

    function check_images2_upload()
        {
        $config['upload_path'] = './public/admin/uploads/';
            $config['allowed_types'] = 'gif|jpg|png|jpeg';
            $config['max_size']   = '10';
            $config['max_width']  = '3000';
            $config['max_height'] = '3000';

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

            if ( ! $this->upload->do_upload('images2'))
            {
                    $this->form_validation->set_message('check_images2_upload', 
                      $this->upload->display_errors());
                     return false;
            }
            else
            {
                    return true;
            }
        }