我在这里做了一个文件下载器,我正在成功下载它下载的excel文件,但当我打开文件时,它给我错误提示下面提到的Excel无法打开该文件,因为文件格式或文件扩展名无效这是怎么回事?
<?php
if (isset($_POST['file_name'])) {
$file_name = $_POST['file_name'];
header('Content-type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$file_name.'"');
readfile('mystery_folder/'.$file_name);
exit();
}
?>
<form action="indexa.php" method="post" name="downloadform">
<input name="file_name" value="My Tracker.xlsx" size="50" type="text">
<input type="submit" value="Download">
</form>
发布于 2016-11-01 13:12:41
在文件名中使用空格不是一个好主意:
value="My Tracker.xlsx"
它可能并没有真正下载该文件,因为无法找到它。您应该重命名不带空格的文件。
发布于 2016-11-01 13:14:32
尝试更改内容类型:
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
并添加:
header('Content-Transfer-Encoding: binary');
此示例适用于我:
$file = 'sample.xlsx';
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
readfile(sprintf('./%s', $file));
https://stackoverflow.com/questions/40360767
复制相似问题