CodeIgniter Laravel PHP Example Javascript jQuery MORE Videos New

How to upload file in CodeIgniter framework


In this example we are going to show you how to how to upload file in CodeIgniter framework.

Here we using 3 files for upload file in CodeIgniter:

  • Upload.php Path: codeIgniter\application\controllers\Upload.php
  • upload_form Path: codeIgniter\application\views\upload_form.php

Create a folder uploads under your project folder.

Upload.php (Controller)

<?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']      = 1024; 
         $config['max_width']     = 1024; 
         $config['max_height']    = 1200;  
         $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());
echo "File uploaded Successfully!"; } } } ?>

upload_form.php (View)

<!DOCTYPE html>
<html>
   <head> 
      <title>Upload Form</title> 
   </head>
	<body>  
      <h3>Your file was successfully uploaded!</h3>  
		
      <ul> 
         <?php foreach ($upload_data as $item => $value):?> 
         <li><?php echo $item;?>: <?php echo $value;?></li> 
         <?php endforeach; ?>
      </ul>  
		
      <p><?php echo anchor('upload', 'Upload Another File!'); ?></p>  
   </body>
</html>

Run the program on your browser with URL:

http://localhost/codeIgniter/index.php/codeIgniter/upload

Here codeIgniter is my folder name. Put your folder name instead of codeIgniter.Rest of things are same.