php - in codeigniter2.1.4,is it mandatory pass the file's field name in do_upload function or not -
i'm curious know in $this->upload->do_upload('img') field name passing mandotory not.i have seen several example in stackoverflow do_upload() not taking argument file field name.but in case of mine without field name file not uploaded.i want know correct syntax?
2)how bypass file upload validation when there no file being uploaded.if there no file(image) in form $this->upload->display_errors() not called.my code below
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); }
1st) no, not necessary, default take name userfile
.
2nd) example fieldname img
check this:
if( $_files['img']['name'] != "" ){ //your upload code here }
Comments
Post a Comment