我有一个表单,它接受一些文本输入字段和一个文件字段(用于上传图像)。
我的问题就是这样,如果我没有填写其中一个必填字段并且我选择了一个有效的图像文件,我将收到有关丢失字段的错误消息,但图像将被上传。控制器是:
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;
}
}
}
据我所知,如果一条规则失败,其他字段和操作将被取消,表格将再次加载,或者其他操作将完成。
谢谢你,,,
答案 0 :(得分:0)
只有在验证为真时才需要上传图像。所以删除你的公共函数upload_image()并在下面给出的验证真实语句中写下功能主义者。
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
}
}
答案 1 :(得分:0)
我发现这篇非常有用的帖子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;
}
}