我正在尝试上传电子表格并使用
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>
//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服务器时,该文件将以随机名称保存在系统的临时文件夹中.
原文链接:https://www.f2er.com/php/137815.html您尝试做的是给出您上传的文件的实际名称,但是因为该文件是在tmp文件夹中使用随机名称创建的.
您将需要使用tmp_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,FALSE); //Insert into database } } else{ echo "Please upload an XLSX or ODS file"; } } else{ echo $_FILES['spreadsheet']['error']; } } } ?>