【问题标题】:Download Excel File from Server has been corrupted - PHP从服务器下载 Excel 文件已损坏 - PHP
【发布时间】:2013-06-11 09:01:12
【问题描述】:

我在从服务器下载 Excel 文件时遇到问题。

excel 文件已经保存在服务器上,我使用下面的代码下载了它。

if(file_exists($reportPath)){
    //content type
    header('Content-type: application/vnd.ms-excel');
    //open/save dialog box
    header('Content-Disposition: attachment; filename='.$dirFile[count($dirFile)-1]);
    //read from server and write to buffer
    readfile($reportPath);
}

但下载的文件已损坏。

我很确定保存在服务器上的文件没有损坏,因为我已经手动将它从服务器获取到我的本地桌面。

意思是,数据已经在运行中被破坏了。

请帮忙,谢谢,我用的是PHP

【问题讨论】:

  • 另请注意,我的代码在我们的服务器上正常,但在另一台服务器上运行不佳

标签: php excel download


【解决方案1】:

你能试试这些标题吗?

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$dirFile[count($dirFile)-1].'"');
header('Cache-Control: max-age=0');

看看它是否有效......干杯!

【讨论】:

    【解决方案2】:

    下载脚本应该是单独的文件。实际上你不应该在这个脚本中打印出任何东西

    //Add below to download the text file created
    $filename = $file; //name of the file
    $filepath = $file; //location of the file. I have put $file since your file is create on the same folder where this script is
    header("Cache-control: private");
    header("Content-type: application/force-download");
    header("Content-transfer-encoding: binary\n");
    header("Content-disposition: attachment; filename=\"$filename\"");
    header("Content-Length: ".filesize($filepath));
    readfile($filepath);
    exit;
    

    【讨论】:

      【解决方案3】:
          $fileName = "data.xls";
          $object_writer = PHPExcel_IOFactory::createWriter($object, 'Excel5');
          ob_end_clean();
          header("Content-Type: application/download");
          header('Content-Disposition: attachment;filename=' . $fileName);
          $object_writer->save('php://output');
      

      使用 ob_end_clean() 清除输出缓冲区。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-12-12
        • 2014-09-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-23
        • 2011-03-30
        相关资源
        最近更新 更多