Codeigniter File Upload

The process of uploading files in CodeIgniter involves two distinct parts that require separate handling—the frontend and the backend. The frontend portion is primarily the responsibility of an HTML form that is equipped with a form input type file feature. Moving onto the backend, the file upload library takes care of formatting the submitted input from the aforementioned form and subsequently inscribing it onto the transfer registry. In this example we are going to show you how to upload image and file in codeigniter 

  • Download Codeigniter
  • Set Base URL and Autoload Helpers
  • Folder Creation
  • Load Library
  • Create controller file
  • Set file upload Preferences
  • Create view file

Download CodeIgniter

    The first step in setting up CodeIgniter is to download it from the official website. You can download the latest version of CodeIgniter 3 from the following URL:

https://codeigniter.com/userguide3/installation/downloads.html

Extract the Files

Once you have downloaded CodeIgniter 3, you need to extract the files to your local machine in a particular folder

Set Base URL and Autoload Helpers

In this step, we need to configure the base URL and autoload helpers in our CodeIgniter 3 application, so open these files and paste the following code
  1. Open the "application/config/config.php" file in your  project
$config['base_url'] = 'http://localhost/codeigniter/';

To autoload a helper in CodeIgniter 3, you need to follow these steps:

  1. Open the "application/config/autoload.php" file in your CodeIgniter 3 project.
  2. Find the line that reads "$autoload['helper'] = array();" and add the name of the helper you want to autoload inside the array.
$autoload['helper'] = array('url','file');

Load Library

Next, open the autoload.php file and load the 'upload' library:


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

Set file upload Preferences

I will also set the preferences for the file upload process through the controller function   do_upload(). this function will contain the following code:


$config['upload_path']   = './uploads/images';
$config['allowed_types'] = 'jpg|png|jpeg';
$config['max_size']      = '2048';


Create new controller

    Next, create new controller file in this path application/controllers/Home.php . Then open the controller file that will display the data and add the following code

Home.php
<?php
class Home extends CI_Controller
{
    public function __construct()
    {
        /*call CodeIgniter's default Constructor*/
        parent::__construct();
        /*load database libray manually*/
        $this->load->database();

    }

    public function index()
    {
        /*load file upload view form*/
        $this->load->view('file_form');
    }

    public function file_upload()
    {
        $files = $_FILES;
        $_FILES['image']['name']= $files['image']['name'];
        $_FILES['image']['type']= $files['image']['type'];
        $_FILES['image']['tmp_name']=$files['image']['tmp_name'];
        $_FILES['image']['error']= $files['image']['error'];
        $_FILES['image']['size']= $files['image']['size'];
        $config['upload_path']   = './uploads/images';
        $config['allowed_types'] = 'jpg|png|jpeg';
        $config['max_size']      = '2048'; // Max size 2Mb
       
        $this->load->library('upload', $config);
        $this->upload->initialize($config);
        if (!$this->upload->do_upload('image')) {
            //log_message('error', 'Image Upload Error: ' . $this->upload->display_errors());
            print_r($this->upload->display_errors());exit;
        }
        else
        {
            $dataInfo = $this->upload->data();
            $image = $dataInfo['file_name'];
            print_r("File Upload Successful");exit;
        }
       
    }

}

?>

Create new view

  1. Create a form in your application that allows users to upload images.
  1. Add the "enctype" attribute to your form tag with the value "multipart/form-data". 

Next, create a new view file in this path "application/views/file_form.php" . Then open the view file that will display the data and add the following code

file_form.php

<!DOCTYPE html>
<html>
<head>
    <title>File Upload form</title>
</head>
<body>

    <form method="post" action="<?= base_url() ?>index.php/home/file_upload" enctype="multipart/form-data">
        <table width="600" border="1" cellspacing="5" cellpadding="5">
            <tr>
                <td>Choose File </td>
                <td><input type="file" name="image" required/></td>
            </tr>
            <tr>
                <td colspan="2" align="center"><input type="submit" name="save" value="Save Data" /></td>
            </tr>
        </table>
    </form>
</body>

</html>

Finally enter your project url in browser: "http://localhost/codeigniter/index.php/home/index"

Conclusion

    We hope this guide has been helpful. If you have any questions or need further assistance, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.