How to upload file/image in CodeIgniter 3

CodeIgniter has an inbuilt upload library that allows uploading files.  You can specify various preferences like – destination path, valid file types, max file size, etc.

In this tutorial, I create a simple example to demonstrate file uploading in CodeIgniter 3.

How to upload file/image in CodeIgniter 3


Contents

  1. Create a folder
  2. Controller
  3. View
  4. Conclusion

1. Create a folder

Navigate to project root directory and create uploads folder to store uploading files from the controller.

How to upload file in CodeIgniter 3


2. Controller

Navigate to application/controllers/ directory and create a User.php file.

On <form> submit define upload file preference e.g. upload folder, allowed file types, max size, file name, etc. Pass this preference while loading upload library.

With $this->upload->do_upload upload file where pass name of file variable as a parameter.

Completed Code

<?php<?phpdefined('BASEPATH') OR exit('No direct script access allowed');
class User extends CI_Controller {
 
 public function index(){
   // load base_url  
   $this->load->helper('url');
 
   // Check form submit or not 
   if($this->input->post('upload') != NULL ){ 
      $data = array(); 
      if(!empty($_FILES['file']['name'])){ 
         // Set preference 
         $config['upload_path'] = 'uploads/'; 
         $config['allowed_types'] = 'jpg|jpeg|png|gif'; 
         $config['max_size'] = '100'; // max_size in kb 
         $config['file_name'] = $_FILES['file']['name']; 

         // Load upload library 
         $this->load->library('upload',$config); 
   
         // File upload
         if($this->upload->do_upload('file')){ 
            // Get data about the file
            $uploadData = $this->upload->data(); 
            $filename = $uploadData['file_name']; 
            $data['response'] = 'successfully uploaded '.$filename; 
         }else{ 
            $data['response'] = 'failed'; 
         } 
      }else{ 
         $data['response'] = 'failed'; 
      } 
      // load view 
      $this->load->view('user_view',$data); 
   }else{
      // load view 
      $this->load->view('user_view'); 
   } 
 }
}

3. View

Navigate to application/views/ directory and create a user_view.php file.

Create a <input type='file'> and button element in the <form>.

Completed Code

<!doctype html>
<html>
 <head>
  <title>How to upload file/image in CodeIgniter 3</title>
 </head>
 <body>
  <b><?php if(isset($response)) echo $response; ?></b>
  <form method='post' action='<?php echo base_url(); ?>' enctype='multipart/form-data'>

   <input type='file' name='file' > <br/><br/>
   <input type='submit' value='Upload' name='upload' />
  </form>
 </body>
</html>

4. Conclusion

For upload a file you need to load upload library and depending on the requirement define your preference.

If you want to upload multiples files then you need to customize the form and loop on the selected files for upload.

You can also view this tutorial to know how to upload a file in CodeIgniter 4.

If you found this tutorial helpful then don't forget to share.