Codeigniter File Uploading Class Library

Codeigniter File Uploading Class Library – This library provides various functions that are used to give permission of files to be uploaded. We can set many preferences, like type and size of files. $this->load->library(‘upload’); is used to load the library. This library is also equip for check file type and file size. Here in this tutorial, we are going to explain how to use file uploading class library.

Codeigniter file uploading class library | Load | Example

Let us understand how file uploading class works in codeigniter with examples.

Load file uploading class library

First load file uploading library to use its functions, this library can be loaded simply as below-

How to load file uploading class library:


class referneces:-

There are following reference in file uploading class.

    • 1. Initialize.
    • 2. Upload
    • 3. Display errors
    • 4. Data

    1. Initialize.

    Syntax of initialize function is

    Syntax of initialize function is:-

    initialize([array $config = array()[$reset = TRUE]])	


    • $config (array) : Preferences
    • $reset (bool) : Whether to reset preferences (that are not provided in $config) to their defaults
    • Returns : CI_Upload instance (method chaining)
    • Return type : CI_Upload

    2. Upload.

    Syntax of Upload function is

    Syntax of Upload function is:-

    do_upload([$field = 'userfile'])	


    • $field (string) : Name of the form field
    • Returns : TRUE on success, FALSE on failure
    • Return type : bool

    3. Display errors.

    Syntax of display errors function is

    Syntax of display errors function is:-

    display_errors([$open = '<p>'[$close = '</p>']])	


    • $open (string) : Opening markup
    • $close (string) : Closing markup
    • Returns : Formatted error message
    • Return type : string

    4. Data.

    Syntax of data function is

    Syntax of data function is:-

    data([$index = NULL])	


    • $data (string) : Element to return instead of the full array
    • Returns : Information about the uploaded file
    • Return type : mixed


    Controller parts:-

    [File Name = upload_controller.php]:-

    defined('BASEPATH') OR exit('No direct script access allowed');
    class upload_controller extends CI_Controller
    public function index()
    public function uploadfile()
    $config['upload_path']          = './uploads/';
    $config['allowed_types']        = 'gif|jpg|png';
    $config['max_size']             = 4096;
    $config['max_width']            = 2000;
    $config['max_height']           = 2000;
    $fileName = 'fileToUpload';
    $this->load->library('upload', $config);
    if ( ! $this->upload->do_upload($fileName))
    $error = $this->upload->display_errors($fileName);
    echo $error;
    $data = $this->upload->data();
    $message = "<span style='color:green'>Successfully Uploaded</span>";
    $this->session->set_flashdata('message', $message);

    View part:-

    [File Name = upload_view.php]:-

    	echo $this->session->flashdata('message'); 
    <?php echo form_open_multipart('index.php/library/upload_controller/uploadfile');?>
    <input type="file" name="fileToUpload" size="20" />
    <br /><br />
    <input type="submit" value="upload" />

    The output will be like this –

    Selected file

    Codeigniter File Uploading Class Library

    Successfull message

    Codeigniter File Uploading Class Library

    File save location

    Codeigniter File Uploading Class Library


    Add Comment

    📖 Read More