现在我正在使用CodeIgniter框架做PHP项目。 现在我正在创建注册表单,需要 4 种类型的输入文件来上传图像。
这是我的视图代码:
Upload image 1 :
<input type="file" class="images" name="image1">
<?php echo form_error('image1'); ?>
Upload image 2 :
<input type="file" class="images" name="image2">
<?php echo form_error('image2'); ?>
这是我的控制器
public function create()
{
$post = $this->input->post();
$res = $this->_register_validate();
if(!$res){
$this->add();
}else{
var_dump("success validation");
exit;
}
}
private function _register_validate()
{
if(!empty($_FILES['image1']['name']))
{
$this->form_validation->set_rules('image1','Upload image 1','callback_check_images1_upload');
}
if(!empty($_FILES['image2']['name']))
{
$this->form_validation->set_rules('image2','Upload image 2','callback_check_images2_upload');
}
$this->form_validation->set_error_delimiters('<span class="error message">', '</span>');
if ($this->form_validation->run() == FALSE)
{
$res = false;
}else{
$res = true;
}
return $res;
}
function check_images1_upload()
{
$config['upload_path'] = './public/admin/uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '10';
$config['max_width'] = '3000';
$config['max_height'] = '3000';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('images1'))
{
$this->form_validation->set_message('check_images1_upload', $data['error'] = $this->upload->display_errors());
return false;
}
else
{
return true;
}
}
function check_images2_upload()
{
$config['upload_path'] = './public/admin/uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '10';
$config['max_width'] = '3000';
$config['max_height'] = '3000';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('images2'))
{
$this->form_validation->set_message('check_images2_upload', $data['error'] = $this->upload->display_errors());
return false;
}
else
{
return true;
}
}
但是显示错误"images2"的结果像这样变成双重
在此处输入图像描述
如果我想显示错误消息从"图像2"变为 1,我该怎么办。
从set_messsage中删除$data['error']
。
public function create()
{
$post = $this->input->post();
$res = $this->_register_validate();
if(!$res){
$this->add();
}else{
var_dump("success validation");
exit;
}
}
private function _register_validate()
{
if(!empty($_FILES['image1']['name']))
{
$this->form_validation->set_rules('image1','Upload image
1','callback_check_images1_upload');
}
if(!empty($_FILES['image2']['name']))
{
$this->form_validation->set_rules('image2','Upload image 2','callback_check_images2_upload');
}
$this->form_validation->set_error_delimiters('<span class="error message">', '</span>');
if ($this->form_validation->run() == FALSE)
{
$res = false;
}else{
$res = true;
}
return $res;
}
function check_images1_upload()
{
$config['upload_path'] = './public/admin/uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '10';
$config['max_width'] = '3000';
$config['max_height'] = '3000';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('images1'))
{
$this->set_message('check_images1_upload', $this->upload-
>display_errors());
return false;
}
else
{
return true;
}
}
function check_images2_upload()
{
$config['upload_path'] = './public/admin/uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '10';
$config['max_width'] = '3000';
$config['max_height'] = '3000';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('images2'))
{
$this->form_validation->set_message('check_images2_upload',
$this->upload->display_errors());
return false;
}
else
{
return true;
}
}