使用php require(filename.php)下载excel文件会导致文件损坏

使用php require(filename.php)下载excel文件会导致文件损坏,php,download,Php,Download,我有一个名为downloadexcel.php的php文件,用于下载一个xlsx文件,如下所示 <?php header('Content-disposition: attachment; filename=mod.xlsx'); header('Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); readfile('mod.xlsx'); exit; 然后下载文件,但当我打

我有一个名为downloadexcel.php的php文件,用于下载一个xlsx文件,如下所示

<?php
header('Content-disposition: attachment; filename=mod.xlsx');
header('Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
readfile('mod.xlsx');
exit;
然后下载文件,但当我打开时出现错误: excel无法打开文件,因为文件格式或文件扩展名无效

值得一提的是,文件是在触发下载之前修改的。另外,不允许直接访问php文件或excel文件的原因是,只有登录的管理员才能访问excel文件


有人知道我怎样才能让它工作吗?

帮自己一个忙,使用PhpExcel。我所有的excel问题都解决了。这可能意味着,您正在执行的脚本需要向浏览器输出一些空格、回显/打印、BOM表标题等内容,然后将这些内容嵌入readfile的数据流中。。。。在文本编辑器中打开该文件并检查您如何修改该文件?我正在使用PHPExcel创建/修改该文件。服务器上的文件没有损坏,只有在我下载时才损坏
require( dirname(__FILE__).DS.'downloadexcel.php' );