How upload file in codeigniter framework?

by scotty_walker , in category: PHP Frameworks , 4 months ago

How upload file in codeigniter framework?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer


by larissa , 4 months ago


To upload a file in CodeIgniter framework, follow these steps:

  1. Set up the file upload configuration in the config.php file located at application/config directory. Update the following configurations as per your requirements: $config['upload_path'] = '/path/to/upload/directory'; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = 1000; $config['max_width'] = 1024; $config['max_height'] = 768;
  2. Create a form in your view file (upload_form.php):
  3. Create a Upload controller in your controllers directory. (application/controllers/Upload.php) and add the following code: load->helper(array('form', 'url')); } public function upload_file() { $config['upload_path'] = '/path/to/upload/directory'; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = 1000; $config['max_width'] = 1024; $config['max_height'] = 768; $this->load->library('upload', $config); if (!$this->upload->do_upload('userfile')) { $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); } } }
  4. Create a upload_success.php view file (application/views/upload_success.php) to display the uploaded file information:
  5. Make sure the upload folder (specified in both config.php and Upload controller) has proper write permissions.

That's it! Now, when you submit the form, it will upload the file to the specified directory and display the uploaded file information on the upload_success.php view file.