PHP下载excel文件已损坏 [英] PHP downloading excel file becomes corrupt
问题描述
我有一个excel文件,我希望用户能够从我的服务器下载。我在这里看了很多问题,但我找不到正确下载文件w / o损坏的方法。我假设它是标题,但我还没有一个工作组合。这是我现在所拥有的,在我收到的损坏的文件中,我可以看到我想要的电子表格的列名称,但它全部搞砸了。
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);
编辑:解决方案我忘了补充说,我在使用Zend在尝试使用本机php方法时会破坏文件。我的修改代码是在我的控制器中放置另一个操作的链接,并从那里下载文件
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);
}
推荐答案
p>尝试这样做
ob_get_clean();
echo file_get_contents($filename);
ob_end_flush();
这篇关于PHP下载excel文件已损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!