0

我很想知道 $this->upload->do_upload('img') 中的字段名没有传递 mandotory。我在 stackoverflow 中看到了几个示例,其中 do_upload() 没有将任何参数作为文件字段名。我的没有字段名称文件没有上传。我想知道哪个是正确的语法?

2)当没有文件上传时如何绕过文件上传验证。如果表单中没有文件(图像),则不会调用 $this->upload->display_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);  
}
4

1 回答 1

0

1st)不,没有必要,默认情况下它将采用 name userfile
2nd)例如说你的字段名是img这样检查的:

if( $_FILES['img']['name'] != "" ){
    //your upload code here
}
于 2013-08-26T08:06:12.600 回答