我有一个表单,它接受一些文本输入字段和文件字段(用于上传图像)。
我的问题是,如果我没有填写必填字段之一,并且我选择了一个有效的图像文件,我会得到一条关于缺少字段的错误消息,但图像将被上载。控制器是:
defined('BASEPATH') OR exit('No direct script access allowed');
class Manage extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$config = array(
array(
'field' => 'item_name',
'label' => 'Item name',
'rules' => 'required'
),
array(
'field' => 'item_code',
'label' => 'Item code',
'rules' => 'required|is_unique[_items.item_code]'
),
array(
'field' => 'item_description',
'label' => 'Item description',
'rules' => 'required'
),
array(
'field' => 'item_img',
'label' => 'Item image',
'rules' => 'callback_upload_image'
)
);
$this->form_validation->set_rules($config);
$this->form_validation->set_message('is_unique', 'Item code (' . $this->input->post('item_code') . ') already exists.');
if($this->form_validation->run() == FALSE)
{
// Render the entry form again
}
else
{
// Go to another page
}
}
public function upload_image()
{
$config['upload_path'] = './items_images';
$config['max_size'] = 1024 * 10;
$config['allowed_types'] = 'gif|png|jpg|jpeg';
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
if(isset($_FILES['item_img']) && !empty($_FILES['item_img']['name']))
{
if($this->upload->do_upload('item_img'))
{
$upload_data = $this->upload->data();
$_POST['item_img'] = $upload_data['file_name'];
return TRUE;
}
else
{
$this->form_validation->set_message('upload_image', $this->upload->display_errors());
return FALSE;
}
}
else
{
$_POST['item_img'] = NULL;
return FALSE;
}
}
}
据我所知,如果一个规则失败,其他字段和操作将被取消,表单将被重新加载,或者其他操作将被完成。
谢谢,,,
发布于 2016-01-09 17:20:15
仅当验证为true时,才需要上传图像。因此,删除您的公共函数upload_image(),并编写如下所示的functionalists validation true语句。
defined('BASEPATH') OR exit('No direct script access allowed');
class Manage extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$config = array(
array(
'field' => 'item_name',
'label' => 'Item name',
'rules' => 'required'
),
array(
'field' => 'item_code',
'label' => 'Item code',
'rules' => 'required|is_unique[_items.item_code]'
),
array(
'field' => 'item_description',
'label' => 'Item description',
'rules' => 'required'
),
array(
'field' => 'item_img',
'label' => 'Item image',
'rules' => 'callback_upload_image'
)
);
$this->form_validation->set_rules($config);
$this->form_validation->set_message('is_unique', 'Item code (' . $this->input->post('item_code') . ') already exists.');
if($this->form_validation->run() == FALSE)
{
// Render the entry form again
}
else
{
$config['upload_path'] = './items_images';
$config['max_size'] = 1024 * 10;
$config['allowed_types'] = 'gif|png|jpg|jpeg';
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
if(isset($_FILES['item_img']) && !empty($_FILES['item_img']['name']))
{
if($this->upload->do_upload('item_img'))
{
$upload_data = $this->upload->data();
$_POST['item_img'] = $upload_data['file_name'];
return TRUE;
}
else
{
$this->form_validation->set_message('upload_image', $this->upload->display_errors());
return FALSE;
}
}
else
{
$_POST['item_img'] = NULL;
return FALSE;
}
}
// Go to another page
}
}
发布于 2017-04-11 17:02:23
我发现这个帖子here非常有帮助。作者编写了自己的规则来验证用户选择上传的文件。代码示例:
$this->form_validation->set_rules('file', '', 'callback_file_check');
public function file_check($str){
$allowed_mime_type_arr = array('application/pdf','image/gif','image/jpeg','image/pjpeg','image/png','image/x-png');
$mime = get_mime_by_extension($_FILES['file']['name']);
if(isset($_FILES['file']['name']) && $_FILES['file']['name']!=""){
if(in_array($mime, $allowed_mime_type_arr)){
return true;
}else{
$this->form_validation->set_message('file_check', 'Please select only pdf/gif/jpg/png file.');
return false;
}
}else{
$this->form_validation->set_message('file_check', 'Please choose a file to upload.');
return false;
}
}
https://stackoverflow.com/questions/34691191
复制相似问题