Download codeigniter file - optional?

I am sure it is simple, but I do not see how to make the file upload with an additional CI.

If you leave the file entry field blank, the message “You did not select a file to download” appears.

The reason I want it to be optional is because my form edits a list of directory types and I don’t have to upload an image every time I edit a list.

Is there a way to remove the “required” error handling in a file class

+7
file upload codeigniter
source share
4 answers

Use the following:

<?php if ( $_FILES AND $_FILES['field_name']['name'] ) { // Upload the file } 
+13
source share

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 ....

+3
source share

Use this code in the controller before calling do_upload ()

 if (is_uploaded_file($_FILES['field_name']['tmp_name'])) { // your code here } 
0
source share

Use this code: -

 $config['upload_path'] = 'assets/img/'; $config['allowed_types'] = 'gif|jpg|png|jpeg'; $this->load->library('upload', $config); // Upload the file if ($this->upload->do_upload('Image')){ $dataimage = $this->upload->data(); $data = array( 'image' => $dataimage['file_name'], 'UserName' => $this->input->post('UserName'), 'Password' => $this->input->post('Password'), 'xid' => $this->input->post('xid') ); } else{ /*$out['msg'] = show_err_msg($this->upload->display_errors()); echo json_encode($out); exit();*/ $data = array( 'image' => NULL, 'UserName' => $this->input->post('UserName'), 'Password' => $this->input->post('Password'), 'xid' => $this->input->post('xid') ); } 
0
source share

All Articles