代码点火器上传文件不起作用


Codeigniter Upload File not working

function submit_article() {
    $this->load->helper(array('form', 'url'));
    $this->load->library('form_validation');
    $this->form_validation->set_error_delimiters('<p style="color:red">', '<br/></p>');
    $my_rules = array(
        array(
            'field' => 'title',
            'label' => 'Title',
            'rules' => 'required|min_length[5]|max_length[20]|xss_clean'
        ),
        array(
            'field' => 'additionalUpload',
            'label' => 'Additional Upload',
            'rules' => 'callback_is_image'
        )
    );
    $this->form_validation->set_rules($my_rules);
    if ($this->form_validation->run() == FALSE) {
        //ERROR
        $data['title'] = ucfirst('submit Article');
        $this->load->view('templates/header', $data);
        $this->load->view('submit_article', $data);
        $this->load->view('templates/footer', $data);
    } else {
        //SUCCESS
        $data['title'] = ucfirst('article Submitted');
        $this->load->view('templates/header', $data);
        $this->load->view('forms_view/submit_article_success', $data);
        $this->load->view('templates/footer', $data);
    }
}
function is_image($value) {
    $config['upload_path'] = './public/uploads/';
    $config['allowed_types'] = 'gif|jpg|png|pdf|tiff';
    $config['max_size'] = '2048';
    $config['max_width'] = '0';
    $config['max_height'] = '0';
    $config['remove_spaces'] = true;
    $this->load->library('upload', $config);
    if (!$this->upload->do_upload()) {
        $this->form_validation->set_message('is_image', $this->upload->display_errors('<p style="color:red">', '<br/></p>'));
        return FALSE;
    } else {
        $this->upload->data();
        return TRUE;
    }
}

大家好,这是我的控制器函数代码,用于在代码点火器中处理多部分表单数据,实际上字段 extraalUpload 不是必填字段,但我希望如果用户在文件类型的 additionalUpload 字段中上传文件,我希望它得到验证,当我运行上面的代码并单击提交按钮而不选择任何文件时,它会显示错误"您没有选择要上传的文件",我不想要因为这不是必填字段,这是我的第一个问题。

第二个是,当我选择一个文件并单击提交按钮时,它再次向我显示"您没有选择要上传的文件"。

备注:我刚刚在这里显示了表单的两个字段,即标题,附加上传,但我总共有9个字段。

提前感谢请帮助任何人。

首先,您必须输入文件字段的名称。

<input type="file" name="image"/>
$this->upload->do_upload('image');

二、不能有max_width和最大高度0

$config['max_width']    = '2048';
$config['max_height']   = '2048';

先尝试一下,然后再看

对于验证字段文件:

if($_FILES['you_field_name']['tmp_name']){
       //your code  
}

一个问候

试试这个检查$_FILES是否不为空,然后做验证,否则什么都不做

 $my_rules = array(
        array(
            'field' => 'title',
            'label' => 'Title',
            'rules' => 'required|min_length[5]|max_length[20]|xss_clean'
        )
    );
if(!empty($_FILES)){
     $my_rules[]= array(
            'field' => 'additionalUpload',
            'label' => 'Additional Upload',
            'rules' => 'callback_is_image'
        )
}
$this->form_validation->set_rules($my_rules);

在上传函数中,您需要指定字段名称

$this->upload->do_upload('your_field_name')