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 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, in this article you will learn how to Download File Using CodeIgniter.

Download File from Database in CodeIgniter: Project Information

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

What is CodeIgniter?

CodeIgniter is an Application Development Framework – a toolset – for PHP website developers.

Its purpose is to let you construct projects much faster than if you were programming code from the 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.

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, make sure that you have sublime or any platform of PHP and MySQL installed on 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 finishing downloading the file, go to the file location, 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, type the URL localhost/phpmyadmin, and create a database.
    download file create database

  • Import Database

    Next, click the created database, click import to the right tab, 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 Files from Database using CodeIgniter?

The code given below is for the file upload module

<!DOCTYPE html>
<html>
<head>
	<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">
</head>
<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">
					<label>Description:</label>
					<input type="text" name="description" class="form-control" required>
				</div>
				<div class="form-group">
					<label>File:</label>
					<input type="file" name="upload" required>
				</div>
				<button type="submit" class="btn btn-primary">Save</button>
			</form>
			<?php
				if($this->session->flashdata('success')){
					?>
					<div class="alert alert-success text-center" style="margin-top:20px;">
						<?php echo $this->session->flashdata('success'); ?>
					</div>
					<?php
				}
 
				if($this->session->flashdata('error')){
					?>
					<div class="alert alert-danger text-center" style="margin-top:20px;">
						<?php echo $this->session->flashdata('error'); ?>
					</div>
					<?php
				}
			?>
		</div>
		<div class="col-sm-8">
			<table class="table table-bordered table-striped">
				<thead>
					<tr>
						<th>ID</th>
						<th>Filename</th>
						<th>Description</th>
						<th>Download</th>
					</tr>
				</thead>
				<tbody>
					<?php
					foreach($files as $file){
						?>
						<tr>
							<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>
						</tr>
						<?php
					}
					?>
				</tbody>
			</table>
		</div>
	</div>
</div>
</body>
</html>

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

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
 
class Files extends CI_Controller {
 
	function  __construct() {
        parent::__construct();
        //load our helper
        $this->load->helper('url');
        //load our model
        $this->load->model('Files_model','files_model');
    }
 
	public function index(){
		//load session library to use flashdata
		$this->load->library('session');
		//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
		$this->load->library('session');
 
	 	//Check if file is not empty
        if(!empty($_FILES['upload']['name'])){
            $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);
            $this->upload->initialize($config);
 
            if($this->upload->do_upload('upload')){
                $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);
				if($query){
					header('location:'.base_url().$this->index());
					$this->session->set_flashdata('success','File uploaded successfully');
				}
				else{
					header('location:'.base_url().$this->index());
					$this->session->set_flashdata('error','File uploaded but not inserted to database');
				}
 
            }else{
              	header('location:'.base_url().$this->index());
              	$this->session->set_flashdata('error','Cannot upload file.'); 
            }
        }else{
            header('location:'.base_url().$this->index());
            $this->session->set_flashdata('error','Cannot upload empty file.');
        }
 
	}
 
	public function download($id){
        $this->load->helper('download');
        $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 the 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.

Conclusion

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.

Inquiries

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