通过验证在codeigniter中上传图像

时间:2015-09-24 12:48:55

标签: php codeigniter

public function store(){                            
        $id=$this->input->post('id'); 
        $_POST['date_created']=date('Y-m-d');       
        if ($this->form_validation->run('registration') === TRUE)
        {                  
            $_POST['password']=  md5($_POST['password']);
            if(!empty($_FILES['profile_img']['name']))
            {             
                echo 'file set ..1<br>';
                if(!$this->upload->do_upload('profile_img'))
                {                                  
                    echo 'file uploading failed ..2';
                    $error = $this->upload->display_errors(); 
                    echo $error;
                    //here its not load the view.
                    $this->template->load('templates/template','emp/add-emp',$error);

                }
                else
                {
                    $img = $this->upload->data();
                    $file_name ='images/'.$img['file_name'];
                    $data=  elements(array('fname','lname','email','password','state','city','mobile','emp_role','profile_img','date_created'),$_POST,$file_name);                    
                }        
            }
            else
            {   
                echo 'file not set..3';
                $data= elements(array('fname','lname','email','password','state','city','mobile','emp_role','date_created'), $_POST);                     
            }  

            if(empty($id)){
                    echo 'Add data ..4';
                    $this->emp_model->set($data);
                   // $this->crud->insert('info',$data);
                    echo '<script type="text/javascript">alert("Inserted Succesfully ");</script>';
                    redirect('employee/add','refresh');
            }
            else{
                    echo 'Update data ..5';
                    $this->emp_model->set($data,$id);
                   // update('info', $data, 'id', $id);
                   //$this->crud->update('info',$data,'id',$id);
                    echo '<script type="text/javascript">alert("Updated Succesfully ");</script>';
                    redirect('employee','refresh');
               }    
        }
        else
        {               
            if(empty($id)){                    
                echo 'add data validation falid ..';
                $this->add();
            }
            else{
                echo 'edit data validation falid ..';
                $this->edit($id);                    
            }          
        }
    }

问题是当文件上传失败意味着当文件不是图像或被删除文件限制时它进入显示错误功能但我无法加载我的视图... 所以请告诉我那里应该有什么问题吗?

1 个答案:

答案 0 :(得分:2)

使用flashdata和重定向可以在保留下一个请求的错误消息时重定向您:

if(! $this->upload->do_upload($file_name){
       $error = $this->upload->display_errors();
       $this->session->set_flashdata('error', '$error');
       redirect('/somewhere', 'refresh');
  }