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"; $filename=$state_id.'.csv'; if (file_exists($file)) { $data = file_get_contents($file); //check file exists force_download($fileName,$data); } else{ echo"not working!"; } }
Where am I going wrong?
Advertisement
Answer
The force_download()
takes in two parameters, i.e. the file name and the data to be written to that file. As the file already consists of some sample data, therefore, the function goes like this:
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 $filename=$state_id.'.csv'; $file="C:\Usersusernew\Desktop\New folder\".$filename; if (file_exists($file)) //check file exists { force_download($file,NULL); //NULL as the file already has data } else{ echo"not working!"; } }