我有2个文本字段和1个文件上传都是必需的。当我只需要文本字段时,一切正常,但是当我需要文件上传时,验证错误仍然表示需要文件,即使我选择了一个文件。任何人都知道我在做什么错了?提前谢谢了。CodeIgniter - 文件上传需要验证
//视图
<?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(); ?>
//控制器
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();
}
}
}
}
我有一个建议,更好地插入你的HTML了。 – Red
好吧,我添加了html,它非常基本,但不知道它是否会有所帮助。 – Staysee
这可能是因为您试图上传太大的文件。尝试上传较小的文件,如果它工作正常,那么您可能需要更改'php.ini'中的设置,以便可以上传更大的文件。另外,如果你想快速调试这个,我建议你在''_FILES'超全局变量上做'var_dump()',看看文件上传的错误代码是什么。 –