What could be the problem with codeigniter force_download()

Viewed 48

I am using Codeigniter and want to achieve download functionality for my project, I am able to run the download functionality on my localhost, but while I try to deploy/integrate that same code on the server it shows undefined function force_download();

The code is running fine on localhost and all the similar configurations have been done on the server as well.

My download controller goes like this:

public function download(){ 
    
    $state_name=$this->input->post('state');
    $year=$this->input->post('year');
    
    $final_name = str_replace(' ', '_', $state_name);   //set name by name convention
    $filename=$final_name.'_'.$year.'.zip';             // set file name with extension
    $this->load->helper('download');                          // Load download helper
    $file="my_path".$filename;                                //set file path
    if($state_name!= ' '&& $year!=' '){
        
        
        if (!empty($file)){                                  //check if the file exists
            force_download($file,NULL);                       
        }
        else{
            echo "File not found";
        }
    }
    else{
        echo "data not recieved in controller";
    }

What could be possibly going wrong with this code even when I have included the helper download in the autoload.php and controller __construct() function.?

Thankful for any contribution in advance.

1 Answers

The force_download() in defined in the helper: download

So, to use the helper download, you need to include it in class contructor.

$this->load->helper('download');

Why do it is working on localhost and not on server:

Localhost might have included the helper download in autoload.php or controller __construct() function.

Related