当我想从服务器下载xls文件时出现奇怪符号错误

时间:2015-11-16 17:07:34

标签: php excel download xls

您好我有这个代码从我的服务器下载xls文件到我的电脑:

<?php

class ControllerDescExcel {

    public function getExcel() {

            $enlace = 'formatoexcel/formatoexcel.xls';
            header ("Content-Disposition: attachment; filename=$enlace ");
            header ("Content-Type: application/force-download");
            header ("Content-Length: ".filesize($enlace));
            readfile($enlace);                

    }

}

?>

但是我的控制台出现了这个错误:

enter image description here

这是xls文件:

enter image description here

抱歉,我的英文。

1 个答案:

答案 0 :(得分:0)

确保脚本没有生成输出,但readfile()内容除外。

我认为文件名可能不包含路径信息。 &#34; file.xls&#34;没问题,&#34; path / file.xmls&#34;是错的。

Content-Type应为application / vnd.ms-excel

public function getExcel() {
    $enlace = 'formatoexcel/formatoexcel.xls';
    $file = basename($enlace); // only use file name not path
    header('Content-type: application/vnd.ms-excel');
    header('Content-Disposition: attachment; filename="' . $file . '"');                  
    header ("Content-Length: ".filesize($enlace));
    readfile($enlace);
}
相关问题