codeigniter force_download() for excel file downloading a corrupt file
Flush the output buffer with the ob_clean() function just before you get your data.
Your code should now look like this.
$this->load->helper('download');$path = base_url('reference/filename.xlsx');ob_clean();$data = file_get_contents($path); // Read the file's contents$name = 'filename.xlsx';force_download($name, $data);
I had a same problem and CI does not support xlsx by default. You will have to find some lib to do this. Anyway, you can download as csv if you want.
I figured out downloading Excel xlsx-files works just fine if you use force_download() inside a model before any html content is loaded. I am using force_download inside my navigation model before any views are loaded.