如何使用 Spout 读取上传的文件($_FILE)



我正在尝试制作一个页面,用户可以在其中上传格式正确的Excel文件(在手册中描述(,并将文件插入数据库。

我想一旦 Spout 可以读取文件,我就可以将文件插入数据库,但该部分不起作用。有人可以帮我吗?

编辑:如果担心,则共享此程序所在的服务器。

法典:

// (1) FILE CHECK
// * HTML file type restriction can still miss at times
if (!isset($_FILES['upexcel']['tmp_name']) || !in_array($_FILES['upexcel']['type'], [
'text/x-comma-separated-values',
'text/comma-separated-values',
'text/x-csv',
'text/csv',
'text/plain',
'application/octet-stream',
'application/vnd.ms-excel',
'application/x-csv',
'application/csv',
'application/excel',
'application/vnd.msexcel',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
])) {
die("Invalid file type");
}
require_once "../database.php";
require_once '/src/Spout/Autoloader/autoload.php';

use BoxSpoutReaderCommonCreatorReaderEntityFactory;
$reader = ReaderEntityFactory::createReaderFromFile($_FILES['upexcel']['tmp_name']);
$reader->open($_FILES['upexcel']['tmp_name']);
foreach ($reader->getSheetIterator() as $sheet) {
foreach ($sheet->getRowIterator() as $row) {
// do stuff with the row
$cells = $row->getCells();
echo "</br> Hello" .$cells;
}
}
$reader->close();

我正在使用 Spout,我遇到了同样的错误,我这样做了,如果您处理该文件是.xlsx类型,它会起作用。

法典:

$file = $_FILES['upexcel'];
$tmpName = $file ['tmp_name'];    
$reader = ReaderEntityFactory::createXLSXReader();
$reader ->open(tmpName);

此代码是一个示例,您只需要更改它:

$reader = ReaderEntityFactory::createReaderFromFile($_FILES['upexcel']['tmp_name']);

为此:

$reader = ReaderEntityFactory::createXLSXReader();

相关内容

最新更新