我对你的代码进行了一些重构,通过分离一些东西,使错误更加明显。等等。我在这里发现了一个错误:
$this->upload->data('userfile')
数据的可选参数应仅为例如be
file_name
或
full_path
但不是文件字段的名称。
https://www.codeigniter.com/userguide3/libraries/file_uploading.html#CI_Upload::data
我也消除了一些
else
语句,首先测试错误,然后在出现错误时退出。如果这仍然失败,你应该取消注释我注释掉的第一行,看看你是否得到了应该得到的输入。请注意,通过ajax发送的文件需要以某种方式进行处理:
$_POST is not working in ajax form submit?
。
function submitjoinusForm() {
//echo '<pre>';
//echo 'post data: <br>';
//print_r($_POST);
//echo 'files: <br>';
//print_r($_FILES);
$response = array();
$oword = $this->session->userdata('captcha_key');
$tword = $this->input->post('captcha');
//form field validation rules
$this->form_validation->set_rules('fullname', 'Name', 'trim|required');
$this->form_validation->set_rules('dob', 'Date of Birth', 'trim|required');
$this->form_validation->set_rules('gender', 'Gender', 'trim|required');
$this->form_validation->set_rules('maritalstatus', 'Marital Status', 'trim|required');
$this->form_validation->set_rules('nationality', 'Nationality', 'trim|required');
$this->form_validation->set_rules('visastatus', 'Visa Status', 'trim|required');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('contact', 'Contact', 'trim|required');
$this->form_validation->set_rules('captcha', 'Captcha', 'trim|required');
if (!$this->form_validation->run()) {
echo json_encode(array('status' => 'alert-danger', 'data' => validation_errors('', '<br>')));
exit;
}
if (strtolower($oword) !== strtolower($tword)) {
echo json_encode(array('status' => 'alert-danger', 'data' => 'Incorrect captcha.'));
exit;
}
$config['upload_path'] = './files/';
$config['allowed_types'] = 'pdf|doc|docx';
$config['max_size'] = 1024 * 3;
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('userfile')) {
$error = 'Please upload your CV file in pdf or word file, maximum size of 2MB. <br>';
$error .= $this->upload->display_errors('', '<br>');
echo json_encode(array('status' => 'alert-danger', 'data' => $error));
exit;
}
$data = array(
'fullname' => $this->input->post('fullname'),
'dob' => $this->input->post('dob'),
'gender' => $this->input->post('gender'),
'maritalstatus' => $this->input->post('maritalstatus'),
'nationality' => $this->input->post('nationality'),
'visastatus' => $this->input->post('visastatus'),
'email' => $this->input->post('email'),
'contact' => $this->input->post('contact'),
//https://www.codeigniter.com/userguide3/libraries/file_uploading.html#CI_Upload::data
'file' => $this->upload->data('file_name')
);
$this->joinusModel->submitJoinus($data);
if ($this->db->insert_id()) {
$response = array(
'status' => 'alert-success',
'data' => "You message has been sent successfully. We'll get back to you shortly.",
);
echo json_encode($response);
} else {
$response = array(
'status' => 'alert-danger',
'data' => "Oops! Something went wrong while sending a message to us.",
);
echo json_encode($response);
}
}