gpt4 book ai didi

php - CodeIgniter - 文件上传需要验证

转载 作者:IT王子 更新时间:2023-10-29 01:06:42 26 4
gpt4 key购买 nike

我有 2 个文本字段和 1 个文件上传,这些都是必需的。当我只需要文本字段时,一切正常,但是当我需要文件上传时,验证错误仍然说需要文件,即使我选择了一个文件。有人知道我做错了什么吗?非常感谢。

// 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();

}
}
}
}

最佳答案

我找到了一个完全符合我要求的解决方案。

我变了

$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', 'required');
}

关于php - CodeIgniter - 文件上传需要验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12289225/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com