PHP downloading excel file becomes corrupt

corrupt, download, excel, php

Solution

try doing it this way

 ob_get_clean();
 echo file_get_contents($filename);
 ob_end_flush();

Problem

I have an excel file that i want a user to be able to download from my server. I have looked at a lot of questions on here but i cannot find a way to correctly download the file w/o corruption. I am assuming it is the headers but i haven't had a working combination of them yet. This is what i have right now and in the corrupt file that i receive i can see the column names of the spreadsheet i want but its all messed up. ``` $filename = '/var/www/web1/web/public/temporary/Spreadsheet.xls'; header("Content-type: application/octet-stream"); header("Content-type: application/vnd-ms-excel"); header("Content-Disposition: attachment; filename=ExcelFile.xls;"); header("Pragma: no-cache"); header("Expires: 0"); readfile($filename); ``` edit: Solution I forgot to add that i was using Zend and it was corrupting the files when trying to use native php methods. My finsihed code was to place a link to another action in my controller and have the files download from there ``` public function downloadAction(){ $file = '/var/www/web1/web/public/temporary/Spreadsheet.xls'; header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment; filename="Spreadsheet.xls"'); readfile($file); // disable the view ... and perhaps the layout $this->view->layout()->disableLayout(); $this->_helper->viewRenderer->setNoRender(true); } ```

Original source

Related problems