在codeigniter中如何在表单验证错误时阻止上传文件?

时间:2018-02-19 10:15:43

标签: php codeigniter

我正在使用Codeigniter 3.x.当我的表单提交,如果表单验证FALSE我上传的文件留在主机上,这是一个错误。我正在使用回调函数进行上传。感谢任何解决方案...

$this->form_validation->set_rules('file', 'lang:pic',callback_multiple_upload');

//The callback

    public function multiple_upload(){

        $this->load->library('upload');
        $number_of_files_uploaded = count($_FILES['file']['name']);
        // Faking upload calls to $_FILE
        for ($i = 0; $i < $number_of_files_uploaded; $i++){
            if(!$_FILES['file']['name'][$i])
                continue;
            $_FILES['file[]']['name']     = $_FILES['file']['name'][$i];
            $_FILES['file[]']['type']     = $_FILES['file']['type'][$i];
            $_FILES['file[]']['tmp_name'] = $_FILES['file']['tmp_name'][$i];
            $_FILES['file[]']['error']    = $_FILES['file']['error'][$i];
            $_FILES['file[]']['size']     = $_FILES['file']['size'][$i];

            $config = array(
                'allowed_types' => 'jpg|jpeg|png|gif',
                'encrypt_name'  => TRUE,
                'max_size'      => 200,
                'remove_spaces' => TRUE,
                'overwrite'     => FALSE,
                'upload_path'   => "./PATH/");
            $this->upload->initialize($config,TRUE);
            if($this->upload->do_upload("file[]")===FALSE){
                $error = $this->upload->display_errors();
                $this->form_validation->set_message('multiple_upload',$error);
                return FALSE;
            }
            else{
                $this->final_files_data[]=$this->upload->data();
            }
        }
        return TRUE;
    }

2 个答案:

答案 0 :(得分:1)

检查验证检查是否返回true。

if ($this->form_validation->run() == TRUE){
    //File uploading
} else {
    //Error message or redirection
}

答案 1 :(得分:0)

修改您的回调函数添加强制性检查。

    public function multiple_upload()
   {
      if (isset($_POST['username'])) // validation checks 
      {
         $this->load->library('upload');
         $number_of_files_uploaded = count($_FILES['file']['name']);
         for ($i = 0; $i < $number_of_files_uploaded; $i++)
         {
            if (!$_FILES['file']['name'][$i])
               continue;
            $_FILES['file[]']['name'] = $_FILES['file']['name'][$i];
            $_FILES['file[]']['type'] = $_FILES['file']['type'][$i];
            $_FILES['file[]']['tmp_name'] = $_FILES['file']['tmp_name'][$i];
            $_FILES['file[]']['error'] = $_FILES['file']['error'][$i];
            $_FILES['file[]']['size'] = $_FILES['file']['size'][$i];

            $config = array(
               'allowed_types' => 'jpg|jpeg|png|gif',
               'encrypt_name' => TRUE,
               'max_size' => 200,
               'remove_spaces' => TRUE,
               'overwrite' => FALSE,
               'upload_path' => "./PATH/");
            $this->upload->initialize($config, TRUE);
            if ($this->upload->do_upload("file[]") === FALSE)
            {
               $error = $this->upload->display_errors();
               $this->form_validation->set_message('multiple_upload', $error);
               return FALSE;
            } else
            {
               $this->final_files_data[] = $this->upload->data();
            }
         }
         return TRUE;
      } else
      {
         return false;
      }
   }