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

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

这是下载后的文件

你可以试试这些标题?

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

看看它是否工作…干杯!

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

 //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;