当我尝试打开从我的应用程序下载的 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";
}khác{
$_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);
....
cơ sở dữ liệu
....
// 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)。我已经尝试更改文件扩展名或修复它,但没有成功
1 Câu trả lời
感谢@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/
Tôi đang chạy PHP trong một tệp JavaScript, như... var = '';). Tôi cần sử dụng JavaScript để quét chuỗi để tìm các ký tự phân cách PHP (mở và đóng PHP). Tôi đã biết cách rồi
Tôi muốn có thể làm điều gì đó như thế này: php --determine-oldest-supported-php-version test.php và nhận được kết quả đầu ra này: 7.2 Nghĩa là nhị phân php được kiểm tra theo thử nghiệm.
Tôi đang phát triển một trang web php lớn hiện không sử dụng bất kỳ framework nào. Câu hỏi lớn của tôi là, liệu có nên thử tích hợp dần dần khung vào ứng dụng theo thời gian, chẳng hạn như tạo các tiện ích mới và cập nhật các tiện ích cũ không? Ví dụ, tất cả các trang đều được phục vụ trực tiếp thông qua URL.
Dưới đây là mã nguồn của tôi, tôi muốn sử dụng biến $r1 nằm trong tập lệnh php ở cuối trong một tập lệnh php khác ở đầu cùng một trang. Tôi cần một giải pháp đơn giản cho vấn đề này. Tôi muốn sử dụng biến đó trong truy vấn cập nhật có trong mã. $tên)
Tôi đang tạo một trang web thực hiện nhiều chuyển hướng PHP tùy thuộc vào các tình huống khác nhau. Giống như thế này... header("Location: somesite.com/redirectedpage.php"); để bảo mật