Codeigniter:图像上传图像验证不起作用

时间:2014-05-30 13:49:06

标签: php codeigniter

我正在尝试检查上传文件字段(<input type="file" name="userfile">)是否为空或图像上传字段上的其他验证。在视图中,我回显<?php echo $upload_error;?>,但它说$upload_error是未定义的变量。我的代码有什么问题,我该如何解决。

这是我的控制器功能

function add_main_products(){
            $this->form_validation->set_rules('product_name', 'Main Product Category', 'required');


            if ($this->form_validation->run() == FALSE){
                $this->load->view('admin/admin_add_mainproduct');
            }
            else{

                $config['upload_path'] = 'application/views/uploads/';               
                $config['allowed_types'] = 'gif|jpg|png|jpeg';                
                $this->load->library('upload', $config);

                if (! $this->upload->do_upload('userfile')){
                    $error['upload_error'] = array('error' => $this->upload->display_errors());

                    $this->load->view('admin/admin_add_mainproduct', $error);

                    return FALSE;
                }

                $this->mod_products->add_main_product($this->upload->data());
                $data = array('upload_data' => $this->upload->data());
                $data['result']="Main Product Added Successfully";
                $this->load->view('admin/admin_add_mainproduct', $data);    
           }
     }

1 个答案:

答案 0 :(得分:0)

不确定我是否理解您的问题,但您可以按照文件输入的相同方式设置规则:

$this->form_validation->set_rules('userfile', 'Image', 'required');

如果有帮助,请告诉我