您好,开发人员我想对具有不同配置的输入字段和2个图像字段进行验证,然后上传图像。上传后,要将这些图像名称保存在数据库中。
public function addcourse()
{
if ($_SERVER['REQUEST_METHOD'] == "POST")
{
$this->form_validation->set_rules('course_name', 'Course
Name', 'trim|required|min_length[2]|max_length[50]', array(
'required' => '%s is required.'
));
$this->form_validation->set_rules('meta_title', 'Meta
Title', 'trim|required', array(
'required' => '%s is required.'
));
$this->form_validation->set_rules('meta_description', 'Meta
'trim | required',
array('required' => ' % sisrequired . '));
if (empty($_FILES['image']['name']))
{
$this->form_validation->set_rules('image', 'Courseimage','trim | required',array('required'=> ' % sisrequired . '));
}
if (empty($_FILES['bannerimage']['name']))
{
{
$this->form_validation->set_rules('bannerimage', 'Courseimage','trim | required',array('required'=> ' % sisrequired . '));
}
if ($this->form_validation->run() == false)
{
$this->add();
}
else
{
$course_name = $this->input->post('course_name');
$meta_title = $this->input->post('meta_title');
$meta_description = $this->input->post('meta_description');
if(isset($_FILES['image']['name']) && $_FILES['image']['name']!='')
{
$config2['upload_path'] = ' . /images / uploads / courses / ';
$config2['allowed_types']= 'gif | jpg | png';
$config2['max_size'] = 1000;
$config2['max_width'] = 2000;
$config2['max_height'] = 2000;
$config2['file_ext_tolower']= true;
$this->upload->initialize($config2);
$this->load->library('upload', $config2);
if (!$this->upload->do_upload('image'))
{
$this->session->set_flashdata("warning",
$this->upload->display_errors());
redirect(base_url('admin / course / add'));
}
else
{
$fileData = $this->upload->data();
$userfile2 = $fileData['file_name'];
}
}
if(isset($_FILES['bannerimage']['name']) &&
$_FILES['bannerimage']['name']!='')
{
$config2['upload_path'] = ' . /images / uploads / banners / ';
$config2['allowed_types']= 'gif | jpg | png';
$config2['max_size'] = 700;
$config2['max_width'] = 1500;
$config2['max_height'] = 800;
$config2['file_ext_tolower']= true;
$this->upload->initialize($config2);
$this->load->library('upload', $config2);
if (!$this->upload->do_upload('bannerimage'))
{
$this->session->set_flashdata("warning",
$this->upload->display_errors());
redirect(base_url('admin / course / add'));
}
else
{
$fileData = $this->upload->data();
$userfile = $fileData['file_name];
}
}
但是代码中正在发生的事情也正在保存并进行验证,但是如果一个文件验证为真,它将上传文件,但是如果第二个失败则显示错误>所以我想要在条件为真时保存两个文件显示验证错误。