File Uploading Class

CodeIgniter is support File Uploading Class permits files to be uploaded. You can set various preferences and restricting the type and size of the files.

Create a view called upload.php. Place the following code in it and save this file to application/views/ directory. upload.php file contains form HTML, it is used to collect the user data and submit to the controller.

<?php echo $this->session->flashdata('success_msg'); ?>
<?php echo $this->session->flashdata('error_msg'); ?>
<form role="form" method="post" enctype="multipart/form-data">
    <div class="panel">
        <div class="panel-body">
            <div class="form-group">
                <input class="form-control" type="file" name="picture" />
             <div class="form-group">
                <input type="submit" class="btn btn-warning" name="userSubmit" value="Upload">

Create a controller called UploadController.php. Place the following code in it and save this file to application/controllers/ directory.

Initially, upload() function will load the upload adding form. When the form is submitted then picture would be uploaded.

<?php defined('BASEPATH') OR exit('No direct script access allowed');
class UploadController extends CI_Controller
    function  __construct() {
    function upload(){
                $config['upload_path'] = 'uploads/';
                $config['allowed_types'] = 'jpg|jpeg|png|gif';
                $config['file_name'] = $_FILES['picture']['name'];
                    $uploadData = $this->upload->data();
                    $this->session->set_flashdata('success_msg', 'User data have been added successfully.');
                    $this->session->set_flashdata('error_msg', 'Some problems occured, please try again.');

Creating Upload Directory in root folder:

Create the uploads/ directory to storing the uploaded images.

After you enter the following URL :