我正在尝试上传电子表格,并使用PHPExcel将其读取到MySQL数据库中。

对于.xlsx文件,它工作正常,但是每当我尝试上载.ods文件时,它都会引发错误:PHP致命错误:在PHPExcel_1.7.9/Classes/PHPExcel/Reader/中的非对象上调用成员函数getNamespaces()第341行的OOCalc.php

怎么了

HTML表单:

<form method="post" enctype="multipart/form-data">
Upload File: <input type="file" name="spreadsheet"/>
<input type="submit" name="submit" value="Submit" />
</form>

PHP(在同一文件中):



//Check valid spreadsheet has been uploaded
if(isset($_FILES['spreadsheet'])){
if($_FILES['spreadsheet']['name']){
    if(!$_FILES['spreadsheet']['error'])
    {

        $inputFile = $_FILES['spreadsheet']['name'];
        $extension = strtoupper(pathinfo($inputFile, PATHINFO_EXTENSION));
        if($extension == 'XLSX' || $extension == 'ODS'){

            //Read spreadsheeet workbook
            try {
                 $inputFile = $_FILES['spreadsheet']['tmp_name'];
                 $inputFileType = PHPExcel_IOFactory::identify($inputFile);
                 $objReader = PHPExcel_IOFactory::createReader($inputFileType);
                 $objPHPExcel = $objReader->load($inputFile);
            } catch(Exception $e) {
                    die($e->getMessage());
            }

            //Get worksheet dimensions
            $sheet = $objPHPExcel->getSheet(0);
            $highestRow = $sheet->getHighestRow();
            $highestColumn = $sheet->getHighestColumn();

            //Loop through each row of the worksheet in turn
            for ($row = 1; $row <= $highestRow; $row++){
                    //  Read a row of data into an array
                    $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
                    //Insert into database
            }
        }
        else{
            echo "Please upload an XLSX or ODS file";
        }
    }
    else{
        echo $_FILES['spreadsheet']['error'];
    }
}
}

?>

最佳答案

当文件上传到您的Web服务器时,该文件将以随机名称保存在系统的临时文件夹中。

您试图做的是提供您上载的文件的实际name,但是由于该文件是在tmp文件夹中以随机名称创建的。
您将需要使用tmp_name代替,它实际上指向该随机命名的文件。

另请注意,在name中,您只有上载文件的名称,而没有路径,
但是,使用tmp_name,您可以找到文件的实际路径。

请参阅以下示例,您将获得文件上传。

array(
 [UploadFieldName]=>array(
    [name] => MyFile.jpg
    [type] => image/jpeg
    [tmp_name] => /tmp/php/php6hst32
    [error] => UPLOAD_ERR_OK
    [size] => 98174
  )
)

将您的代码更改为此
 //Check valid spreadsheet has been uploaded
if(isset($_FILES['spreadsheet'])){
if($_FILES['spreadsheet']['tmp_name']){
if(!$_FILES['spreadsheet']['error'])
{

    $inputFile = $_FILES['spreadsheet']['tmp_name'];
    $extension = strtoupper(pathinfo($inputFile, PATHINFO_EXTENSION));
    if($extension == 'XLSX' || $extension == 'ODS'){

        //Read spreadsheeet workbook
        try {
             $inputFileType = PHPExcel_IOFactory::identify($inputFile);
             $objReader = PHPExcel_IOFactory::createReader($inputFileType);
                 $objPHPExcel = $objReader->load($inputFile);
        } catch(Exception $e) {
                die($e->getMessage());
        }

        //Get worksheet dimensions
        $sheet = $objPHPExcel->getSheet(0);
        $highestRow = $sheet->getHighestRow();
        $highestColumn = $sheet->getHighestColumn();

        //Loop through each row of the worksheet in turn
        for ($row = 1; $row <= $highestRow; $row++){
                //  Read a row of data into an array
                $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
                //Insert into database
        }
    }
    else{
        echo "Please upload an XLSX or ODS file";
    }
}
else{
    echo $_FILES['spreadsheet']['error'];
}
}
}

?>

关于php - 使用PHPExcel读取电子表格,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21507898/

10-12 15:02