我创建了一个包含2个文件上传字段的表单,基本上我想让它们成为必填字段。我创建了一个回调函数,但似乎无法让它正常工作。它正在使用回调并发布错误,如果其他一些字段留空,但发送表单是否附加文件。 我对Codeigniter很新。非常感谢任何帮助!
控制器:
class Form extends CI_Controller {
function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
//Upload errors array
$up_errors = array();
$this->form_validation->set_rules('first_name', 'First Name', 'required|alpha');
$this->form_validation->set_rules('last_name', 'Surname', 'required|alpha');
$this->form_validation->set_rules('dob', 'Date of Birth', 'required');
$this->form_validation->set_rules('nationality', 'Nationality', 'required|alpha');
$this->form_validation->set_rules('gender', 'Gender', 'required');
$this->form_validation->set_rules('address_l1', 'Address Line 1', 'required|alpha_dash_space');
//$this->form_validation->set_rules('address_l2', 'Address Line 2', 'alpha');
$this->form_validation->set_rules('address_city', 'City', 'required|alpha');
$this->form_validation->set_rules('address_postcode', 'Post Code', 'required|alpha_dash_space');
//$this->form_validation->set_rules('address_country', 'Country', 'required|alpha_dash_space');
$this->form_validation->set_rules('e_address', 'Email Address', 'required|valid_email');
$this->form_validation->set_rules('h_tel', 'Home Telephone Number', 'required|numeric');
$this->form_validation->set_rules('mobile', 'Mobile Number', 'required|numeric');
$this->form_validation->set_rules('university', 'University', 'required|alpha_dash_space');
$this->form_validation->set_rules('campus', 'Campus Name', 'required|alpha_dash_space');
$this->form_validation->set_rules('course', 'Course Title', 'required|alpha_dash_space');
$this->form_validation->set_rules('end_date', 'Course End Date', 'required');
//Custom callback
$this->form_validation->set_rules('file', 'Attachment', 'callback_handle_upload');
//Check if file attached
//if (isset($_FILES['file']))
//{
//}
if ($this->form_validation->run() == FALSE)
{
$this->load->view('home');
}
else
{
//Display Success page
$this->load->view('formsuccess');
//Array helper
$this->load->helper('array');
//Set form data array
$fields = $this->input->post('first_name')."\n".
$this->input->post('last_name')."\n".
$this->input->post('dob')."\n".
$this->input->post('nationality')."\n".
$this->input->post('gender')."\n".
$this->input->post('address_l1')."\n".
$this->input->post('address_l2')."\n".
$this->input->post('address_city')."\n".
$this->input->post('address_postcode')."\n".
$this->input->post('address_country')."\n".
$this->input->post('e_address')."\n".
$this->input->post('h_tel')."\n".
$this->input->post('mobile')."\n".
$this->input->post('university')."\n".
$this->input->post('campus')."\n".
$this->input->post('course')."\n".
$this->input->post('end_date');
$msg = serialize($fields);
//Upload files to server
$this->load->library('upload');
//Send Email
$this->load->library('email');
//Set config
$config['upload_path'] = './attachments'; //if the files does not exist it'll be created
$config['allowed_types'] = 'gif|jpg|png|doc|docx|txt|pdf';
$config['max_size'] = '4000'; //size in kilobytes
$config['encrypt_name'] = TRUE;
$this->upload->initialize($config);
$uploaded = $this->upload->up(FALSE); //Pass true if you want to create the index.php files
//Attach the 2 files to email
foreach($_FILES as $key => $value)
{
//var_dump($uploaded['success'][$key]['full_path']); //FOR TESTING
$file = $uploaded['success'][$key]['full_path'];
$this->email->attach($file);
//unlink($file);
}
//var_dump($msg); //FOR TESTING
$this->email->from($this->input->post('e_address'),$this->input->post('first_name') . $this->input->post('last_name'));
$this->email->to('tom@shu.ac.uk');
$this->email->subject('NON-SHU STUDENT REQUEST');
$this->email->message($msg);
$this->email->send();
//echo $this->email->print_debugger();
}
}
function alpha_dash_space($str_in)
{
if (! preg_match("/^([-a-z0-9_ ])+$/i", $str_in)) {
$this->form_validation->set_message('_alpha_dash_space', 'The %s field may only contain alpha-numeric characters, spaces, underscores, and dashes.');
return FALSE;
} else {
return TRUE;
}
}
function handle_upload()
{
if (count($_FILES['file']['name'] < 2)
{
// throw an error because nothing was uploaded
$this->form_validation->set_message('handle_upload', "You must attach both files!");
return false;
}
else
{
return TRUE;
}
}
} ?&GT;
答案 0 :(得分:0)
检查以下表单验证扩展,它可以帮助您验证文件,带有CI表单验证库的映像复制代码并在应用程序库中创建MY_Form_validation.php文件并将代码粘贴到该文件中并保存