我正在尝试创建一个页面,用户可以在其中上传格式正确的 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 Box\Spout\Reader\Common\Creator\ReaderEntityFactory;
$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();