CodeIgniter Download File From Database With Source Code

About The Project

This CodeIgniter Download File From Database will show you how to use CodeIgniter to download/force download files from your database.

To force download files, we’ll use the CodeIgniter’s “force download()” class in this tutorial. The MVC (Model-View-Controller) architecture is used by CodeIgniter, a lightweight PHP framework.

A Download File In CodeIgniter was developed using CodeIgniter and MySQL Database as Back-End, This article you will learn on how to Download File Using CodeIgniter.

Project Information’s

Project Name:CodeIgniter Download File From Database With Source Code
Language/s Used:PHP with CodeIgniter Web Framework
PHP version (Recommended):5.6.3
Type:Web Application
File Download in PHP CodeIgniter– Project Information

What is CodeIgniter?

CodeIgniter is an Application Development Framework – a toolset – for PHP web site developers. Its purpose is to let you construct projects much faster than if you were programming code from start by providing a rich set of libraries for common activities, as well as a simple interface and logical structure to access these libraries.

By reducing the amount of code required for a given operation, CodeIgniter allows you to focus more creatively on your project.

In this Project also includes a downloadable CodeIgniter Project With Source Code for free, just find the downloadable source code below and click to start downloading.

To start executing this Download File From Database With Source Code, makes sure that you have sublime or any platform of PHP and MySQL installed in your computer.

Steps On How To Run The CodeIgniter Download File From Database With Source Code

Time needed: 5 minutes.

These are the steps on how to run CodeIgniter Download File From Database With Source Code.

  • Download Source Code

    First, find the downloadable source code below and click to start downloading the source code file.
    download source code

  • Extract File

    Next, after finished to download the file, go to file location and right click the file and click extract.
    download file extract file

  • Copy Project Folder

    Next, copy the project folder and paste it to C:\xampp\htdocs.
    download file copy folder

  • Open Xampp

    Next, open xampp and start the apache and mysql.
    download file open xampp

  • Create Database

    Next, click any browser and type to the URL localhost/phpmyadmin and create database.
    download file create database

  • Import Database

    Next, click the created database and click import to the right tab and click choose file and import the sql file inside the download folder.
    download file import sql file

  • Execute Project

    final, type to the URL localhost/codeigniter_download.
    download file run project

How To Download File From Database Using CodeIgniter?

The code given below is for the file upload module

<!DOCTYPE html>
	<meta charset="utf-8">
	<title>CodeIgniter Download File (IT SOURCECODE)</title>
	<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>bootstrap/css/bootstrap.min.css">
<body class="bg-info">
<div class="container">
	<h1 class="page-header text-center">CodeIgniter Download File (IT SOURCECODE)</h1>
	<div class="row">
		<div class="col-sm-4">
			<h3>File Upload Form</h3>
			<form method="POST" action="<?php echo base_url(); ?>files/insert" enctype="multipart/form-data">
				<div class="form-group">
					<input type="text" name="description" class="form-control" required>
				<div class="form-group">
					<input type="file" name="upload" required>
				<button type="submit" class="btn btn-primary">Save</button>
					<div class="alert alert-success text-center" style="margin-top:20px;">
						<?php echo $this->session->flashdata('success'); ?>
					<div class="alert alert-danger text-center" style="margin-top:20px;">
						<?php echo $this->session->flashdata('error'); ?>
		<div class="col-sm-8">
			<table class="table table-bordered table-striped">
					foreach($files as $file){
							<td><?php echo $file->id; ?></td>
							<td><?php echo $file->filename; ?></td>
							<td><?php echo $file->description; ?></td>
							<td><a href="<?php echo base_url().'files/download/'.$file->id; ?>" class="btn btn-success btn-sm"><span class="glyphicon glyphicon-download-alt"></a></td>


The code given below is for the controller module for uploading and downloading file

defined('BASEPATH') OR exit('No direct script access allowed');
class Files extends CI_Controller {
	function  __construct() {
        //load our helper
        //load our model
	public function index(){
		//load session library to use flashdata
		//fetch all files i the database
		$data['files'] = $this->files_model->getAllFiles();
		$this->load->view('file_upload', $data);
	public function insert(){
		//load session library to use flashdata
	 	//Check if file is not empty
            $config['upload_path'] = 'upload/';
            //restrict uploads to this mime types
            $config['allowed_types'] = 'jpg|jpeg|png|gif';
            $config['file_name'] = $_FILES['upload']['name'];
            //Load upload library and initialize configuration
            $this->load->library('upload', $config);
                $uploadData = $this->upload->data();
                $filename = $uploadData['file_name'];
				//set file data to insert to database
				$file['description'] = $this->input->post('description');
				$file['filename'] = $filename;
				$query = $this->files_model->insertfile($file);
					$this->session->set_flashdata('success','File uploaded successfully');
					$this->session->set_flashdata('error','File uploaded but not inserted to database');
              	$this->session->set_flashdata('error','Cannot upload file.'); 
            $this->session->set_flashdata('error','Cannot upload empty file.');
	public function download($id){
        $fileinfo = $this->files_model->download($id);
        $file = 'upload/'.$fileinfo['filename'];
        force_download($file, NULL);

Project Output

File Download In CodeIgniter Output
File Download In CodeIgniter Output

Download Source Code below

Anyway, if you want to level up your programming knowledge, especially PHP, try this new article I’ve made for you Best PHP Projects With Source Code Free Download 2021.


This lesson did not cover all that a full-fledged tutorial would, but it did introduce you to the most important modules, developing controllers, and models.

We hope that our CodeIgniter instructional Download File From Database offered you some insight into some of CodeIgniter’s basic design patterns, which you may build on.


If you have any questions or suggestions about CodeIgniter Download File From Database With Source Code, please feel free to leave a comment below.

1 thought on “CodeIgniter Download File From Database With Source Code”

Leave a Comment