Where am I doing wrong in downloading a file with ‘.CSV’ format in CodeIgniter’s controller?

I am new to CodeIgniter and was working on downloading a file. However, I want to download a file that resides on my local machine, I am able to locate the file by providing the path, also the file gets downloaded with a File type, However, I want my file to be in .csv format

Here goes my Controller’s download function:

public function download()
        $state_id=$this->input->post('state'); // gets me the state-id from viw's dropdown
        $this->load->helper('download'); // Load download helper
        $file="C:\Usersusernew\Desktop\New folder\".$state_id.".csv";
        if (file_exists($file))
                $data = file_get_contents($file);       //check file exists 
                echo"not working!";

Where am I going wrong?


As the force_download() function accepts the file name to be set and the data to insert into that file, as you have the data in that file already, you just need to download it. so, You should use the for_download() function like this:


This will solve your problem 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *