PhpSpreadsheet部署Ubuntu服务器后类找不到问题及解决
PhpSpreadsheet部署到Ubuntu服务器后出现类找不到的致命错误
本地通过Composer安装PhpSpreadsheet后代码可正常运行,但部署到DigitalOcean的Ubuntu服务器后,触发以下PHP致命错误:
[Thu Oct 20 02:05:24.760534 2022] [php:error] [pid 817] [client 181.129.171.154:7921] PHP致命错误:未捕获的Error:在/var/www/Ausen2/logic/ausen_excel.php第16行找不到类“PhpOffice\PhpSpreadsheet\SpreadSheet”
堆栈跟踪:
#0 {main}
在/var/www/Ausen2/logic/ausen_excel.php第16行抛出
相关代码
<?php require '../vendor/autoload.php'; use PhpOffice\PhpSpreadsheet\{SpreadSheet, IOFactory}; use PhpOffice\PhpSpreadsheet\Writer\Xlsx; // 支持Csv、Xls格式 require("../db.php"); session_start(); $sqli = $_SESSION['ausen_list']; //$nombre_admin = $_SESSION['NOM_USUARIO']; //$id_admin = $_SESSION['ID_USUARIO']; // 创建Excel实例 $excel =new SpreadSheet(); // 设置元数据 $excel -> getProperties()->setCreator("Andres")->setTitle("我的Excel"); $excel->setActiveSheetIndex(0); $hojaActiva = $excel->getActiveSheet(); $hojaActiva->setTitle("员工列表"); // 设置表头信息 $hojaActiva->setCellValue('A1','员工证件号'); $hojaActiva->getColumnDimension('A')->setWidth(15); $hojaActiva->setCellValue('B1','开始日期'); $hojaActiva->getColumnDimension('B')->setWidth(15); $hojaActiva->setCellValue('C1','结束日期'); $hojaActiva->getColumnDimension('C')->setWidth(15); $hojaActiva->setCellValue('D1','时长'); $hojaActiva->setCellValue('E1','备注'); $hojaActiva->getColumnDimension('E')->setWidth(20); $hojaActiva->setCellValue('F1','工作安全'); $hojaActiva->getColumnDimension('F')->setWidth(15); $hojaActiva->setCellValue('G1','用户ID'); $hojaActiva->getColumnDimension('G')->setWidth(15); $hojaActiva->setCellValue('H1','缺勤类型'); $hojaActiva->getColumnDimension('H')->setWidth(15); $ausentismos = $conectar->query($sqli); $fila = 2; while($ausentismo = $ausentismos->fetch_assoc()){ $hojaActiva->setCellValue('A'.$fila, $ausentismo['Cedula_F']); $hojaActiva->setCellValue('B'.$fila, $ausentismo['Fecha_Inicio']); $hojaActiva->setCellValue('C'.$fila, $ausentismo['Fecha_Fin']); $hojaActiva->setCellValue('D'.$fila, $ausentismo['Tiempo']); $hojaActiva->setCellValue('E'.$fila, $ausentismo['Observacion']); $hojaActiva->setCellValue('F'.$fila, $ausentismo['Seguridad_Trabajo']); $hojaActiva->setCellValue('G'.$fila, $ausentismo['ID_Usuario']); if($ausentismo['Tipo_Ausentismo'] == 1){ $hojaActiva->setCellValue('H'.$fila, '病假'); }elseif ($ausentismo['Tipo_Ausentismo'] == 2){ $hojaActiva->setCellValue('H'.$fila, '调休'); }elseif ($ausentismo['Tipo_Ausentismo'] == 3){ $hojaActiva->setCellValue('H'.$fila, '事假'); }elseif ($ausentismo['Tipo_Ausentismo'] == 4){ $hojaActiva->setCellValue('H'.$fila, '休假'); } $fila++; } // 输出Excel文件 header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Disposition: attachment;filename="报告.xlsx"'); header('Cache-Control: max-age=0'); $writer = IOFactory::createWriter($excel, 'Xlsx'); $writer->save('php://output'); exit; ?>
排查步骤
已尝试以下操作但未解决问题:
- 重新安装Composer
- 执行
composer dump-autoload命令
问题原因与解决方案
问题根源是文件系统大小写敏感性差异:
- 本地环境(通常为Windows,文件系统大小写不敏感)中,使用
SpreadSheet(第二个字母大写S)可以正常匹配类文件 - Ubuntu服务器(Linux系统,文件系统大小写敏感)中,PhpSpreadsheet库的实际类名为
Spreadsheet(第二个字母小写s)
只需修改代码中所有的SpreadSheet为Spreadsheet即可解决:
- 更新use导入语句:
use PhpOffice\PhpSpreadsheet\{Spreadsheet, IOFactory}; - 修改实例化代码:
$excel = new Spreadsheet();
内容的提问来源于stack exchange,提问作者AndresDM
相关产品推荐
相关产品推荐

