0

I am having problem dealing with File Uploads in CodeIgniter 2.2. I am able to create its specific folder destination, but I am unable to upload the file that I have selected.

Here's my Controller:

function create_report() { if($this->session->userdata('logged_in')) { $this->create_model->set_report(); $this->session->set_flashdata('message', 'Success! You created a Report!'); #$redirect($_SERVER['HTTP_REFERER'], 'refresh'); } else { $this->session->set_flashdata('message', 'Oops! You have to Login'); //If no session, redirect to login page redirect('login', 'refresh'); } } 

and here is my Model:

function set_report() { if($this->session->userdata('logged_in')) { $session_data = $this->session->userdata('logged_in'); if($_FILES['userfile']['name'] != NULL) { $main_dir = './FILES/'.$this->input->post('patientname').'/'; // Check if User Folder is already created. Create New if none exist if(!is_dir($main_dir)){ mkdir($main_dir, 0777); } $target_dir = './FILES/'.$this->input->post('patientname').'/'.$this->input->post('session_id').'/'; // Check if Session Folder is already created. Create New if none exist if(!is_dir($target_dir)) { mkdir($target_dir, 0777); } $config['upload_path'] = './FILES/'.$this->input->post('patientname').'/'.$this->input->post('session_id').'/'; $config['allowed_types'] = 'gif|jpg|png|docx|xlsx|doc|pdf|csv|zip|rar|7zip|ppt|pptx'; $this->load->library('upload', $config); $data2 = array('upload_data' => $this->upload->data()); } $data = array( 'session_id' => $this->input->post('session_id'), 'report_creator' => $session_data['username'], 'report_patientname' => $this->input->post('patientname'), 'report_patientid' => $this->input->post('patientid'), 'report_subject' => $this->input->post('subject'), 'report_description' => $this->input->post('description'), 'report_time' => $this->input->post('date'), 'report_date' => $this->input->post('time') ); return $this->db->insert('session_reports', $data); } } 

I've been trying to solve this and I haven't figure out the key. I hope anyone could help me with this.

6
  • So ? What is your error ? Did you try to display $this->upload->display_errors() to see what's wrong ? Commented Aug 10, 2015 at 9:08
  • By the way, you forgot $this->upload->do_upload($field_name) to upload your file. Read the documentation : ellislab.com/codeigniter/user-guide/libraries/… Commented Aug 10, 2015 at 9:09
  • are you sure mkdir($main_dir, 0777) is working ?? Commented Aug 10, 2015 at 9:18
  • @VincentDecaux well there is no error showing, I also tried doing ` $field_name = "userfile"; $this->upload->do_upload($field_name); ` Commented Aug 10, 2015 at 9:19
  • @Abdulla. Yes. It is working Commented Aug 10, 2015 at 9:21

2 Answers 2

1

I believe, I have failed to initialize the config.

$this->upload->initialize($config); 

That solved my problem. Thank you everyone

Sign up to request clarification or add additional context in comments.

Comments

0

Remove this

$data2 = array('upload_data' => $this->upload->data()); 

and replace this to that

if (!$this->upload->do_upload()) { $error = array('error' => $this->upload->display_errors()); $this->load->view('upload_form', $error); } else { $data = array('upload_data' => $this->upload->data()); $this->load->view('upload_success', $data); } 

Load Library

$this->load->library('upload', $config); 

Alternately you can set preferences by calling the initialize() method. Useful if you auto-load the class:

$this->upload->initialize($config); 

Codeigniter file Upload

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.