Php 在codeigniter2.1.4中,是否必须传递文件&x27;是否在do_upload函数中使用字段名

Php 在codeigniter2.1.4中,是否必须传递文件&x27;是否在do_upload函数中使用字段名,php,codeigniter,file-upload,Php,Codeigniter,File Upload,我很想知道$this->upload->do_upload('img')中的字段名传递的是mandotory not。我在stackoverflow中看到过几个例子,其中do_upload()不使用任何参数作为文件字段名。但是如果我没有字段名,则file not upload。我想知道哪种语法正确 2) 如果表单中没有文件(图像),那么$this->upload->display\u errors()将不会被调用。我的代码如下 function add() { if ($this->

我很想知道$this->upload->do_upload('img')中的字段名传递的是mandotory not。我在stackoverflow中看到过几个例子,其中do_upload()不使用任何参数作为文件字段名。但是如果我没有字段名,则file not upload。我想知道哪种语法正确

2) 如果表单中没有文件(图像),那么$this->upload->display\u errors()将不会被调用。我的代码如下

function add()
{
    if ($this->input->server('REQUEST_METHOD') === 'POST')
    {
     $this->form_validation->set_rules('category', 'Category Name', 'required');
     if ($this->form_validation->run())
     {
        $data_to_store = array(
             'category' => $this->input->post('category'),
            'description' => $this->input->post('description'),
            'parent'=>'0'
         );
        $last_id=$this->admin_category_model->add_category($data_to_store);

        $config['upload_path'] ='./uploads/';
        $config['allowed_types'] = 'gif|jpg|png|GIF|JPG|PNG';
        $config['remove_spaces'] = TRUE;
        $config['max_size'] = '0';
        $config['file_name'] =$last_id.'_'.$_FILES['img']['name'];
        $config['overwrite'] = TRUE;
        $this->load->library('upload', $config);
        if($this->upload->do_upload('img'))
        {
            $data = array('upload_data' => $this->upload->data());
            $config2['image_library'] = 'gd2';
             $config2['source_image'] = $data['upload_data']['full_path'];
            $config2['new_image'] ='./uploads/thumb/'.$data['upload_data']['file_name'];
            $config2['create_thumb'] = FALSE;
            $config2['maintain_ratio'] = TRUE;
            $config2['width'] = 35;
             $config2['height'] = 35;
            $this->load->library('image_lib',$config2);
            $this->image_lib->resize(); 
            $data_to_store = array(
             'img' => $config['file_name'],
             );
           $this->admin_category_model->update_category($last_id,$data_to_store);
           $this->session->set_flashdata('flash_message', 'Record Added');
            redirect('admin_category/index');
        }
         else
        {
         $data['error']=$this->upload->display_errors();
        }

     }

    }
    $data['title']='Add Category';
    $data['main_content'] = 'admin/add_category';
    $this->load->view('admin/includes/template', $data);  
}
1)不,不需要,默认情况下它将采用名称
userfile

2)例如,说你的字段名是
img
检查如下:

if( $_FILES['img']['name'] != "" ){
    //your upload code here
}

如果我使用userfile而不是img,那么在$this->upload->do_upload()中可以使用吗?是的,如果你想在不提供文件名的情况下使用do_upload()函数。在第二点中,我正在搜索是否存在任何codeigniter语法?不,我不知道。如果有帮助,请接受答案。