In this tutorial we are going to learn CodeIgniter File Uploading. We can upload files using the File Uploading class and we can also limit the type and size of the file to upload. Follow the steps shown in the example to understand CodeIgniter file upload process.

CodeIgniter File Uploading Example

Copy and save the following code to application/view/upload_form.php file.

<!DOCTYPE html>
<html>
<head>
  <title>TutorialPace.com | Upload Form</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" crossorigin="anonymous">

</head>
<body class="container">
  <div class="row mt-3">
  <?php echo $error;?> 
    
      <form action="/upload/do_upload" enctype="multipart/form-data" method="post" accept-charset="utf-8">
      	<div class="form-group">
      		<input type = "file" name = "userfile" size = "10" class="form-control" /> 
      	</div>
        <div class="form-group">
      		<input type = "submit" value = "upload" class="btn btn-info" /> 
      	</div>
      </form> 
  </div>
</body>
</html>

Copy and save the following code to application/view/Upload_success.php file.

<!DOCTYPE html>
<html>
<head>
  <title>TutorialPace.com | File Uploaded</title>
   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" crossorigin="anonymous">
</head>
<body class="container">
    <h3 class="h3">File is successfully uploaded!</h3>  
    
      <ul> 
      <?php foreach ($upload_data as $item => $value) {
          echo '<li> '.$item.' : '.$value.' </li>';
      }?>
      </ul>   
      <p><?php echo anchor('upload', 'Upload Another File!..', array('class' => 'btn btn-info')); ?></p>
</body>
</html>

Copy and save the code shown below to application/controllers/Upload.php. Create the folder “uploads” in the CodeIgniter root i.e. At the application folder parent directory.

<?php
  
   class Upload extends CI_Controller {
  
      public function __construct() { 
         parent::__construct(); 
         $this->load->helper(array('form', 'url')); 
      }
    
      public function index() { 
         $this->load->view('upload_form', array('error' => ' ' )); 
      } 
    
      public function do_upload() { 
         $config['upload_path']   = './uploads/'; 
         $config['allowed_types'] = 'gif|jpg|png'; 
         $config['max_size']      = 10000; 
         $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['upload_data'] = $this->upload->data(); 
            $this->load->view('upload_success', $data); 
         } 
      } 
   } 
?>

In application/config/routes.php, make the following change to the route file and add the following line at the end of the file.

$route['upload'] = 'Upload';

Now let’s run this example by visiting the URL in the browser. The first page will be off upload as you can see in the following image.

CodeIgniter File Uploading

You will see the next screen after you have successfully uploaded a file.

CodeIgniter File Uploading