【发布时间】:2012-02-23 10:27:44
【问题描述】:
我有一个 Excel 文件,我希望用户能够从我的服务器下载该文件。我在这里查看了很多问题,但我找不到正确下载文件而不会损坏的方法。我假设它是标题,但我还没有它们的有效组合。这就是我现在拥有的,在我收到的损坏文件中,我可以看到我想要的电子表格的列名,但它都搞砸了。
$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 方法时它损坏了文件。我完成的代码是在我的控制器中放置一个指向另一个操作的链接,并从那里下载文件
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);
}
【问题讨论】:
-
下载我猜你的意思是在浏览器中显示?而不是直接下载文件
-
输出 2 个内容标题是没有意义的——一个文件不能同时是两个东西。这就像在说“我是长颈鹿和鲸鱼!”。
-
很长很长的路要走,对这类问题最常见的答案是:a) 在
<?php ?>标签之前/之后有前导/尾随空格/HTML您的脚本或文件中的 BOM,或者 b) 您忘记在readfile()之后调用exit/die并且在文件数据的末尾输出了更多内容。 -
@MarcCostello 我的意思是直接下载。
-
@DaveRandom 我在声明中遗漏了一个退出,这将阻止我将文件放在一起,但文件仍然损坏。
标签: php excel download corrupt