从服务器下载Excel文件已损坏 - PHP

时间:2013-06-11 09:01:13

标签: php excel download

我有从服务器下载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

this is the file after downloaded

2 个答案:

答案 0 :(得分: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');

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

答案 1 :(得分:1)

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

//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;
相关问题