the problem is how to validate image and form data on same time
问题是如何在同一时间验证图像和表格数据
$this->load->library('form_validation');
$this->form_validation->set_message('file_check', 'jpg attachement allowed Only');
$this->form_validation->set_rules('file', '', 'callback_file_check');
$this->form_validation->set_rules('number','Number','trim|required|min_length[10]|max_length[10]');
if($this->form_validation->run()){
$name=$this->input->post('name');
$subject=$this->input->post('subject');
$messege=$this->input->post('messege');
$number=$this->input->post('number');
$configs['upload_path'] = './uploads/';
$configs['allowed_types'] = 'jpg';
$this->load->library('upload', $configs);
//upload file to directory
if($this->upload->do_upload('file') ){
$upload_data = $this->upload->data();
$this->load->library('email',$config);
$this->email->attach($upload_data['full_path']);
$this->email->set_newline("\r\n");
$email_body ="<div >".$messege."</div>";
$this->email->set_mailtype("html");
$this->email->from('mail@gmail.com',$name);
$this->email->to('mailto@gmail.com');
$this->email->subject($subject);
$this->email->message($email_body);
$this->email->send();
$this->index();
}
}
1 个解决方案
#1
0
You can do it with custom validation function.
您可以使用自定义验证功能。
function do_upload()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('file', 'File', 'callback_file_check');
$this->form_validation->set_rules('number','Number','trim|required|min_length[10]|max_length[10]');
if($this->form_validation->run()==TRUE)
{
//upload image
}
else
{
// return form error
}
}
function file_check($str){
// load file helper
$this->load->helper('file');
// image mime type
$allowed_mime_type_arr = array('image/gif','image/jpeg','image/pjpeg','image/png','image/x-png');
$mime = get_mime_by_extension($_FILES['file']['name']);
// file is uploaded and not empty
if(isset($_FILES['file']['name']) && $_FILES['file']['name']!="")
{
if(in_array($mime, $allowed_mime_type_arr))
{
return TRUE;
}
else
{
// return false with custome error message
$this->form_validation->set_message('file_check', 'Please select only gif/jpg/png file.');
return FALSE;
}
}
else
{
$this->form_validation->set_message('file_check', 'Please choose a file to upload.');
return FALSE;
}
}
#1
0
You can do it with custom validation function.
您可以使用自定义验证功能。
function do_upload()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('file', 'File', 'callback_file_check');
$this->form_validation->set_rules('number','Number','trim|required|min_length[10]|max_length[10]');
if($this->form_validation->run()==TRUE)
{
//upload image
}
else
{
// return form error
}
}
function file_check($str){
// load file helper
$this->load->helper('file');
// image mime type
$allowed_mime_type_arr = array('image/gif','image/jpeg','image/pjpeg','image/png','image/x-png');
$mime = get_mime_by_extension($_FILES['file']['name']);
// file is uploaded and not empty
if(isset($_FILES['file']['name']) && $_FILES['file']['name']!="")
{
if(in_array($mime, $allowed_mime_type_arr))
{
return TRUE;
}
else
{
// return false with custome error message
$this->form_validation->set_message('file_check', 'Please select only gif/jpg/png file.');
return FALSE;
}
}
else
{
$this->form_validation->set_message('file_check', 'Please choose a file to upload.');
return FALSE;
}
}