doudao7511 2018-10-16 07:08
浏览 47

图像上传的错误消息在codeigniter中显示如此之多

now i am doing php project using CodeIgniter framework. now i am creating registration form which need 4 types input file for upload image.

here is my view code :

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'); ?> 

and here is my controllers

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

but the result for display error "images2" became double like this

enter image description here

what should i do if i want to display error message from "images2" become 1.

  • 写回答

1条回答 默认 最新

  • duanbeng1923 2018-10-16 08:50
    关注

    Remove $data['error'] from set_messsage.

      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;
                }
            } 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看