【问题标题】:PHP - excel file downloadPHP - excel文件下载
【发布时间】:2020-12-28 01:40:55
【问题描述】:

我从服务器下载文件,但下载后 excel 或 word 文件损坏。 Pdf、jpg等都可以。

ob_clean();
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: ").$att->getType();
header("Content-Disposition: attachment; filename=" . basename($att->getName()));  
header("Content-Transfer-Encoding: binary");
header("Content-Description: File Transfer");
readfile($path);

我拥有的 excel 内容类型 - application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

【问题讨论】:

    标签: php


    【解决方案1】:

    只是改变

    header("Content-Type: ").$att->getType();
    

    header("Content-Type: ".$att->getType());
    

    您发送的文件内容类型为空...

    【讨论】:

    • 你是对的,但我也试过 header("Content-Type: application/octet-stream");或 header("Content-Type: application/vnd.ms-excel");或 header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") 结果相同
    【解决方案2】:

    对于excel和word,换个试试

    header("Content-Type: ").$att->getType();
    

    header("Content-Type: application/octet-stream");
    
    

    或者,对于excel

    header("Content-Type: application/vnd.ms-excel"); 
    

    或者,对于单词

    header("Content-Type: application/msword");
    

    【讨论】:

    • 不,我以前也有过,但没用
    • 对于 png 和 zip,你应该分别使用 Content-type: image/png 和 Content-Type: application/zip 但如果出现问题(比如你的情况),请尝试 octet-stream 或Content-Type:application/vnd.ms-excel 或 application/msword(请尝试我修改后的答案)
    【解决方案3】:

    问题不同...我刚刚在我的 php 文件中添加了新行之后?> 感谢 Amin 和 Ken 的帮助

    【讨论】:

    • 很高兴知道您已经解决了这个问题。祝你有美好的一天。
    猜你喜欢
    • 2020-01-13
    • 1970-01-01
    • 2017-12-18
    • 2012-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-01
    相关资源
    最近更新 更多