codeigniter file upload optional ... works fine ..... :)
---------- controller ---------
function file() { $this->load->view('includes/template', $data); } function valid_file() { $this->form_validation->set_rules('userfile', 'File', 'trim|xss_clean'); if ($this->form_validation->run()==FALSE) { $this->file(); } else { $config['upload_path'] = './documents/'; $config['allowed_types'] = 'gif|jpg|png|docx|doc|txt|rtf'; $config['max_size'] = '1000'; $config['max_width'] = '1024'; $config['max_height'] = '768'; $this->load->library('upload', $config); if ( !$this->upload->do_upload('userfile',FALSE)) { $this->form_validation->set_message('checkdoc', $data['error'] = $this->upload->display_errors()); if($_FILES['userfile']['error'] != 4) { return false; } } else { return true; } }
I just use these lines, which do this optionally,
if($_FILES['userfile']['error'] != 4) { return false; } $_FILES['userfile']['error'] != 4 is for file required to upload.
you can make it unnecessary with $_FILES['userfile']['error'] != 4 , then it will pass this error to the file required, and works fine with other types of errors, if any, using return false , hope that this works for u ....
Chirag rafaliya
source share