我有此 PHP 代码,用于使用 xampp 中的 PhpSpreadsheet 将 excel 导入 mysql。此代码在 Windows 环境中运行时没有问题,但如果我使用 XAMPP Linux 将其托管到 Linux 服务器(Ubuntu 22.04)上,则会出现错误“HTTP ERROR 500”。我已根据 phpspreadsheet.readthedocs.io 文档安装了 PhpSpreadsheet 所需的所有软件包和扩展。如果此错误是针对服务器端的,我该怎么办?请帮忙
这些是我的导入文件的代码
composer.json
{
"name": "mark/import1",
"autoload": {
"psr-4": {
"Mark\\Import1\\": "src/"
}
},
"authors": [
{
"name": "mark"
}
],
"require": {
"phpoffice/phpspreadsheet": "^1.28"
},
"config": {
"platform": {
"php": "8.1"
}
}
}
导入代码
<?php
$con = mysqli_connect('localhost','root','','db_import');
require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
if(isset($_POST['save_excel_data']))
{
$fileName = $_FILES['import_file']['name'];
$file_ext = pathinfo($fileName, PATHINFO_EXTENSION);
$allowed_ext = ['xls','csv','xlsx'];
if(in_array($file_ext, $allowed_ext))
{
$inputFileNamePath = $_FILES['import_file']['tmp_name'];
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($inputFileNamePath);
$data = $spreadsheet->getActiveSheet()->toArray();
foreach($data as $row)
{
$fullname = $row['0'];
$email = $row['1'];
$phone = $row['2'];
$course = $row['3'];
$studentQuery = "INSERT INTO students (fullname,email,phone,course)
VALUES('$fullname','$email','$phone','$course')";
$result = mysqli_query($con, $studentQuery);
$msg = true;
}
}
}