Fatal error: Call to undefined function force_download()

Fatal error: Call to undefined function force_download()

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

While working on file download functionality using codeigniter , I am getting below fatal error: Call to undefined function force_download() , Refer below stack trace for more details.

tags codeigniter php

Error

Fatal error: Call to undefined function force_download() in C:\phpdevelopment\codeigniterexamples\application\controllers\FileController.php on line 21
A PHP Error was encountered
Severity: Error
Message: Call to undefined function force_download()
Filename: controllers/FileController.php
Line Number: 21
Backtrace:

Download Controller Class

<?php
defined('BASEPATH') or exit('No direct script access allowed');
class FileController extends CI_Controller{  
    public function __construct(){
        parent::__construct();
    }
    
    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());
            }
        }
    }
}

Solution

To use force_download() function, You have to load codeigniter download helper class $this->load->helper('download'); in download() function or in controller __construct() function.

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]