SpreadsheetReader 对象创建无一例外地失败

SpreadsheetReader Object creation failing without exception

我正在尝试将我的 excel 文件上传到 MySQL 数据库并使用 Spout 来执行此操作.这是我的 HTML 和 PHP.

代码
<!DOCTYPE html>
    <html>
        <head>
            <title>Excel Uploading PHP</title>
            <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
        </head>
        <body>
            <div class="container">
            <h1>Excel Upload</h1>
                <form method="POST" action="upload_excel.php" enctype="multipart/form-data">
                    <div class="form-group">
                        <label>Upload Excel File</label>
                        <input type="file" name="file" class="form-control">
                    </div>
                    <div class="form-group">
                        <button type="submit" name="Submit" class="btn btn-success">Upload</button>
                    </div>
                </form>
            </div>
        </body>
    </html>

这是PHP代码

<?php

use Box\Spout\Reader\ReaderFactory;
use Box\Spout\Common\Type;

echo 'File Used';

// Include Spout library 
require_once 'Spout/Autoloader/autoload.php';
require 'SpreadsheetReader.php';    
echo 'Require complete';
require('db_connection.php');

// check file name is not empty
if (!empty($_FILES['file']['name'])) {
    // Get File extension eg. 'xlsx' to check file is excel sheet
    $pathinfo = pathinfo($_FILES["file"]["name"]);
    print_r($pathinfo);

    // check file has extension xlsx, xls and also check 
    // file is not empty
if (($pathinfo['extension'] == 'xlsx' || $pathinfo['extension'] == 'xls') 
        && $_FILES['file']['size'] > 0 ) {
        echo 'File validated';
        $inputFileName = 'uploads/'.$_FILES['file']['name'];

        move_uploaded_file($_FILES['file']['tmp_name'], $inputFileName);
        $Reader = new SpreadsheetReader($inputFileName);

        echo $inputFileName;
        try
        {
            $reader = ReaderFactory::create(Type::XLSX);
            echo 'Reader successful';
        }
        catch(Exception $e)
        {
            echo $e->getMessage();
        }
        // Read excel file by using ReadFactory object.

        echo 'opening file';
        // Open file
        $reader->open($inputFileName);
        $count = 1;

        // Number of sheet in excel file
        foreach ($reader->getSheetIterator() as $sheet) {

            // Number of Rows in Excel sheet
            foreach ($sheet->getRowIterator() as $row) {

                // It reads data after header. In the my excel sheet, 
                // header is in the first row. 
                if ($count > 1) { 

                    // Data of excel sheet
                    $data['<column_name>'] = $row[0];
                    $data['<column_name>'] = $row[1];
                    $data['<column_name>'] = $row[2];
                    $data['<column_name>'] = $row[3];
                    $data['<column_name>'] = $row[3];
                    $data['<column_name>'] = $row[3];
                    $data['<column_name>'] = $row[3];
                    $data['<column_name>'] = $row[3];
                    $data['<column_name>'] = $row[3];
                    $data['<column_name>'] = $row[3];

                    $query = "<SQL QUERY GOES HERE>";
                    $result = mysqli_query($conn, $query);

                    print_r(data);

                }
                $count++;
            }
        }

        // Close excel file
        $reader->close();

    } else {

        echo "Please Select Valid Excel File";
    }

} else {

    echo "Please Select Excel File";

}
?>

我写了一些 PHP echo' 来检查程序的流程。该文件在以下行之前完美运行:move_uploaded_file($_FILES['file']['tmp_name'], $inputFileName); 并在那之后停止执行。

它复制给定文件夹中的文件并在尝试创建 SpreadsheetReader 对象时停止。我试图在它周围放置 Try-Catch 块以捕获任何异常错误,但也没有捕获任何异常。

让我告诉您安装状态:

以上所有内容都托管在 VPS 上,运行 上托管在 Ubuntu 18.10 上。 spout 不是使用 composer 安装的,我已经从预安装的源中复制了文件。

我是否遗漏了任何必须安装在 OS 上才能解决此问题的软件包?还是程序本身缺少什么?我搜索了参考资料,但根据文章,这段代码似乎有效。我很困惑,这里的问题在哪里?

请有人指导我寻找或至少给我一个工作参考 Excel-MySQL 文件上传。

谢谢

SpreadsheetReader 不是 Spout 的 class。尝试实例化此 class 是行不通的,因为您没有 'require' 它。这就是你的程序在那里崩溃的原因。你可以删除这一行,因为我没有看到它被使用。

我也看到了这个:if (($pathinfo['extension'] == 'xlsx' || $pathinfo['extension'] == 'xls')

Spout 仅支持 XLSX 文档。所以如果你给它传递一个 XLS 文档,它就不会工作。