PEAR Spreadsheet Excel Writer本地文件写入报错求助
错误分析
从堆栈跟踪来看,核心问题是tempnam()创建临时文件后返回了空路径,导致后续fopen('', 'w+b')触发ValueError: Path cannot be empty:
Notice: tempnam(): file created in the system's temporary directory in /Applications/XAMPP/xamppfiles/htdocs/_test/vendor/pear/ole/OLE/PPS/File.php on line 105
Fatal error: Uncaught ValueError: Path cannot be empty in /Applications/XAMPP/xamppfiles/htdocs/_test/vendor/pear/ole/OLE/PPS/File.php:106
Stack trace:
#0 /Applications/XAMPP/xamppfiles/htdocs/_test/vendor/pear/ole/OLE/PPS/File.php(106): fopen('', 'w+b')
#1 /Applications/XAMPP/xamppfiles/htdocs/_test/vendor/pear/spreadsheet_excel_writer/Spreadsheet/Excel/Writer/Workbook.php(599): OLE_PPS_File->init()
#2 /Applications/XAMPP/xamppfiles/htdocs/_test/vendor/pear/spreadsheet_excel_writer/Spreadsheet/Excel/Writer/Workbook.php(576): Spreadsheet_Excel_Writer_Workbook->_storeOLEFile()
#3 /Applications/XAMPP/xamppfiles/htdocs/_test/vendor/pear/spreadsheet_excel_writer/Spreadsheet/Excel/Writer/Workbook.php(235): Spreadsheet_Excel_Writer_Workbook->_storeWorkbook()
#4 /Applications/XAMPP/xamppfiles/htdocs/_test/content/xls/xls_common.php(292): Spreadsheet_Excel_Writer_Workbook->close()
#5 /Applications/XAMPP/xamppfiles/htdocs/_test/content/showcontent.php(439): require_once('/Applications/X...')
#6 {main} thrown in /Applications/XAMPP/xamppfiles/htdocs/_test/vendor/pear/ole/OLE/PPS/File.php on line 106
这不是你修改的pear/ole/OLE/PPS文件夹权限问题,而是PHP临时目录的配置或权限异常导致的。
解决方案
1. 检查并修复PHP临时目录配置
- 打开XAMPP的PHP配置文件:
/Applications/XAMPP/xamppfiles/etc/php.ini - 找到以下两个配置项,确保它们指向存在且有读写权限的目录:
upload_tmp_dir = "/Applications/XAMPP/xamppfiles/temp" sys_temp_dir = "/Applications/XAMPP/xamppfiles/temp" - 确认目标目录存在并设置正确权限:
sudo mkdir -p /Applications/XAMPP/xamppfiles/temp sudo chmod 755 /Applications/XAMPP/xamppfiles/temp sudo chown _www:_www /Applications/XAMPP/xamppfiles/temp - 重启XAMPP的Apache服务。
2. 在代码中手动指定临时目录
如果修改php.ini不方便,可以在导出Excel的代码开头添加以下代码,强制设置PHP临时目录:
// 指定一个你确认有读写权限的临时目录 ini_set('sys_temp_dir', '/Applications/XAMPP/xamppfiles/temp'); ini_set('upload_tmp_dir', '/Applications/XAMPP/xamppfiles/temp');
3. 考虑兼容性替代方案
PEAR的Spreadsheet_Excel_Writer是老旧组件,对PHP 8的兼容性有限。如果上述方法无效,建议替换为维护更活跃的PhpSpreadsheet(无需PEAR依赖),它支持PHP 8+且功能更完善。
内容的提问来源于stack exchange,提问作者Dominique Radisson

