【问题标题】:Error message for image upload display so many in codeigniter在 codeigniter 中显示太多图片上传的错误信息
【发布时间】:2018-10-16 16:50:37
【问题描述】:

现在我正在使用 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,我应该怎么做。

【问题讨论】:

  • 在return语句后添加exit;
  • 这是行不通的。问题还是一样
  • 尝试打印 $data['error'].

标签: php codeigniter


【解决方案1】:

从 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;
            }
        } 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-27
    • 2017-05-28
    • 2012-03-05
    • 2017-10-04
    • 2020-02-07
    • 2020-12-31
    相关资源
    最近更新 更多