CodeIgniter - 文件上传需要验证 [英] CodeIgniter - File upload required validation
本文介绍了CodeIgniter - 文件上传需要验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有2个文本字段和1个文件上传,都是必需的。一切都工作,当我只需要文本字段,但是当我需要文件上传验证错误仍然说,一个文件是必需的,即使我选择一个。任何人都知道我做错了什么?非常感谢。
//查看
< ;? php echo form_open_multipart('add'); >
< fieldset>
< input type =textname =name/>< br>
< input type =textname =code/>< br>
< input type =filename =userfile/>< br>< br>
< input type =submitvalue =Add/>
< / fieldset>
<?php echo form_close(); >
// controller
public function add(){
$ this-> form_validation-> set_rules('name','Name','required');
$ this-> form_validation-> set_rules('code','Code','required');
// $ this-> form_validation-> set_rules('userfile','Document','required');
//当上面的行是活动的上传不通过
if($ this-> form_validation-> run()== FALSE){
$ data ['page_view'] = $ this-> page_view;
$ data ['page_title'] = $ this-> page_title;
$ this-> load-> view('template',$ data);
}
else
{
$ this-> load-> library('upload');
if(!empty($ _ FILES ['userfile'] ['name']))
{
$ config ['upload_path'] ='./uploads/' ;
$ config ['allowed_types'] ='gif | jpg | png | jpeg';
$ this-> upload-> initialize($ config);
if($ this-> upload-> do_upload('userfile'))
{
$ img = $ this-> upload-> data ;
$ file_name = $ img ['file_name'];
$ name = $ this-> input-> post('name');
$ code = $ this-> input-> post('code');
$ this-> load-> model('create','create_model');
$ this-> create_model-> create_entry($ name,$ code,$ file_name);
$ data ['page_view'] = $ this-> page_view;
$ data ['page_title'] = $ this-> page_title;
$ this-> load-> view('template',$ data);
}
else
{
echo $ this-> upload-> display_errors();
}
}
}
}
我改变了
$ b $ b
$ this-> form_validation-> set_rules('name','Name','trim | required');
$ this-> form_validation-> set_rules('code','Code','trim | required');
$ this-> form_validation-> set_rules('userfile','Document','required');
至
$ this-> form_validation-> set_rules('name','Name','trim | required');
$ this-> form_validation-> set_rules('code','Code','trim | required');
if(empty($ _ FILES ['userfile'] ['name']))
{
$ this-> form_validation-> set_rules('userfile','Document' '需要');
}
I have 2 text fields and 1 file upload that are all required. Everything works when I require just the text fields, but when I require the file upload the validation error stays saying that a file is required, even though I selected one. Anyone know what I am doing wrong? Many thanks in advance.
//view
<?php echo form_open_multipart('add'); ?>
<fieldset>
<input type="text" name="name" /><br>
<input type="text" name="code" /><br>
<input type="file" name="userfile" /><br><br>
<input type="submit"value="Add" />
</fieldset>
<?php echo form_close(); ?>
//controller
public function add() {
$this->form_validation->set_rules('name', 'Name', 'required');
$this->form_validation->set_rules('code', 'Code', 'required');
//$this->form_validation->set_rules('userfile', 'Document', 'required');
//when the above line is active the upload does not go through
if ($this->form_validation->run() == FALSE) {
$data['page_view'] = $this->page_view;
$data['page_title'] = $this->page_title;
$this->load->view('template', $data);
}
else
{
$this->load->library('upload');
if (!empty($_FILES['userfile']['name']))
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$this->upload->initialize($config);
if ($this->upload->do_upload('userfile'))
{
$img = $this->upload->data();
$file_name = $img['file_name'];
$name = $this->input->post('name');
$code = $this->input->post('code');
$this->load->model('create', 'create_model');
$this->create_model->create_entry($name, $code, $file_name);
$data['page_view'] = $this->page_view;
$data['page_title'] = $this->page_title;
$this->load->view('template', $data);
}
else
{
echo $this->upload->display_errors();
}
}
}
}
解决方案
I found a solution that works exactly how I want.
I changed
$this->form_validation->set_rules('name', 'Name', 'trim|required');
$this->form_validation->set_rules('code', 'Code', 'trim|required');
$this->form_validation->set_rules('userfile', 'Document', 'required');
To
$this->form_validation->set_rules('name', 'Name', 'trim|required');
$this->form_validation->set_rules('code', 'Code', 'trim|required');
if (empty($_FILES['userfile']['name']))
{
$this->form_validation->set_rules('userfile', 'Document', 'required');
}
这篇关于CodeIgniter - 文件上传需要验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文