gpt4 book ai didi

PHP : Excel cannot open the file because the file format or file extension is not valid

转载 作者:行者123 更新时间:2023-12-04 10:19:14 30 4
gpt4 key购买 nike

当我尝试打开从我的应用程序下载的 xls 文件时,出现此错误:

excel cannot open the file because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.

这是我的 PHP 代码:首先,我上传 xls 文件:

if (is_uploaded_file($_FILES["file_name"]["tmp_name"])) {
if (rename($_FILES["file_name"]["tmp_name"], $directoryDestination.$nameDestination)) {
import::importFile($link,$directoryDestination.$nameDestination, $importLib, $excelName,$enddate);
$_SESSION['message'] = "ok";
}else{
$_SESSION['erreur'] = "Error";
}

然后在我的函数中:

use PhpOffice\PhpSpreadsheet\IOFactory;

importFile($link,$filename, $importLib, $excelName,$enddate){
header('Content-Type: text/html; charset=utf-8');
// loading Excel file
$objPHPExcel = IOFactory::load($filename);
....
database
....
// copy the file
mkdir($new_path,0777);
chmod($new_path, 0777);
copy($filename,$new_path.$excelName);
unlink($filename);

// then , in other file, I try to download the file
$contents = file_get_contents($filepath);
$mime = mime_content_type($filepath);
$size = filesize($filepath);
$parts = explode("/", $file);
$filename = end($parts);

header("Content-Type: {$mime}");
header("Content-disposition: attachment; filename=\"" . basename($filename) . "\"");
header("Content-Length: {$size}");
print($contents);
exit();

我使用的是 MS 2016 (PhpOffice\PhpSpreadsheet)。我已经尝试更改文件扩展名或修复它,但没有成功

最佳答案

感谢@Danny Battison当我用 Notpad 打开文件时,我看到文件顶部有空白,所以我在之前添加了 ob_end_clean() ( doc ):

ob_end_clean();
header("Content-Type: {$mime}");

它工作正常。

关于PHP : Excel cannot open the file because the file format or file extension is not valid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60941026/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com