1

谁能帮我在 PHP 上编写代码以从服务器下载 Excel 文件。

我使用以下代码创建了以下代码headerreadfile但下载的文件已损坏。

//content type
header('Content-type: application/vnd.ms-excel');
//open/save dialog box
header('Content-Disposition: attachment; filename='.$fileName);
//read from server and write to buffer
readfile($reportPath);

任何人都可以帮助我从服务器下载现有 Excel 文件的最佳方式。

下载后请看下图数据在此处输入图像描述

4

3 回答 3

2
ob_clean();

将该代码放在所有标题声明之前

于 2013-06-11T14:02:04.230 回答
0

我建议使用 X-SENDFILE 标头。https://tn123.org/mod_xsendfile/

于 2013-06-11T13:05:10.160 回答
0

我使用如下内容:

header("Content-Description: File Transfer");
header("Content-Type: application/octet-stream");
header('Content-Disposition: attachment; filename="'.basename($path).'"');
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header("Content-Type: application/force-download");
header("Content-Type: application/download");
header("Content-Length: ".filesize($path));
readfile($path);
exit;

确保在 readfile 之前或之后没有输出任何内容。

于 2013-06-11T13:06:47.583 回答