Codeigniter File Download Example

Codeigniter File Download Example

By Yashwant Chavan, Views 50940, Date 25-Aug-2016

In this tutorial I will show you how to download file using codeigniter. This code is pretty much simple to understand, Here we are using force_download() function, which will help to download the file content from server location.

tags codeigniter php

Tools and Technologies

To execute this example, I am using latest version of codeigniter framework 3.1.0

  1. Php Codeigniter 3.1.0
  2. XAMPP / Php 5.6

Refer below image to understand codeigniter directory structure.

Codeigniter File Download Project setup in Eclipse

As you know that Php Codeigniter framework is popular for minimum configuration (almost Zero configuration). To execute this example, you have do below configurations in config.php file. Refer base url and route configurations.

Codeigniter base url configuration

$config['base_url'] = "http://localhost/codeigniterexamples";

Codeigniter routes configuration

Here I have defined below routes to connect to respected download controller. You can configure routes as per your requirement.

$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['download/(:any)'] = "/filecontroller/download/$1";

FileController.php

Lets create the FileController class under folder name application/controllers along with index() and download(fileName = NULL) functions.

Load codeigniter download helper $this->load->helper('download') class in controller construct function.

download() - function has one parameter called as fileName, Which we are going to read or download the from server location. First we will check if specified is exist or not using file_exists() function, If it is available then download it, otherwise redirect to base url.

file_get_contents() - function is used to read the file contents.

force_download() - function is used to download the file, Which also generates respected headers that force to download the file.

If you want to directly download the file using server path or URL , then use force_download('/path/to/abc.zip', NULL);

<?php
 defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
 class FileController extends CI_Controller {
  public function __construct() {
   parent::__construct ();
   $this->load->helper('download');
  }

  public function index() {
   $this->load->view ( 'welcome', $data );
  }

  public function download($fileName = NULL) {   
   if ($fileName) {
    $file = realpath ( "download" ) . "\\" . $fileName;
    // check file exists    
    if (file_exists ( $file )) {
     // get file content
     $data = file_get_contents ( $file );
     //force download
     force_download ( $fileName, $data );
    } else {
     // Redirect to base url
     redirect ( base_url () );
    }
   }
  }
 }

welcome.php

Create welcome.php file under folder application/views. Just simply add the download button along with file URL.

<!DOCTYPE html>
<html lang=en>

<head>
    <meta charset=utf-8>
    <meta http-equiv=X-UA-Compatible content="IE=edge">
    <meta name=viewport content="width=device-width,initial-scale=1">

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://code.jquery.com/jquery-3.1.0.min.js" integrity="sha256-cCueBR6CsyA4/9szpPfrX3s49M9vUU5BgtiJj06wt/s=" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>


</head>

<body>
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <h1>File download example using Codeigniter</h1>
                <a href="<?=base_url ()?>index.php/download/imp.zip" class="btn btn-primary">Download imp.zip</a>
            </div>
        </div>
    </div>
</body>

</html>

Run

Enter the url http://localhost/codeigniterexamples in your browser, It will show you the default page content. Once you click on (Download imp.zip) button, It will start downloading the imp.zip file.

Codeigniter File Download Example

Finish

Hope this Tutorial is useful to you, Thanks

Yashwant

Yashwant

Hi there! I am founder of technicalkeeda.com and programming enthusiast. My skills includes Java,J2EE, Spring Framework, Nodejs, PHP and lot more. If you have any idea that you would want me to develop? Lets connect: yashwantchavan[at][gmail.com]