This article mainly introduces about TP5 import database, has a certain reference value, now share to everyone, the need for friends can refer to
HTML page Code theme:
<form method= "POST" action= "{: url (' Index/savestudentimport ')}" class= "Form-signin" enctype= "Multipart/form-data "> <p> <select name=" Themeid "> {volist name=" theme "id=" V "} <option value=" {$ V.id} ">{$v .title}</option> {/volist} </select> </p><br/> <input Name= "Excel" type= "file" class= "Form-control" > <br/><br/> <button class= "Btn Btn-lg Btn-primary btn-block "> Import </button> </form>
Controller Receive code:
Public Function Savestudentimport () {//import (' Phpexcel. Phpexcel ', extend_path);//Method Two vendor ("Phpexcel.phpexcel"); Method One $objPHPExcel = new \phpexcel (); $themeid =input (' Post.themeid ');//Import data type//Get form upload file $file = Request ()->file (' Excel '); $info = $file->validate ([' Size ' =>1567800, ' ext ' = ' xlsx,xls,csv '])->move (Root_path. ' Public '. Ds. ' Excel '); if ($info) {$exclePath = $info->getsavename (); Gets the file name $file _name = Root_path. ' Public '. Ds. ' Excel '. Ds. $exclePath; The address of the uploaded file $objReader =\phpexcel_iofactory::createreader (' Excel5 '); $obj _phpexcel = $objReader->load ($file _name, $encode = ' utf-8 '); Load file contents, encode utf-8 echo "<pre>"; $excel _array= $obj _phpexcel->getsheet (0)->toarray (); Convert to array format array_shift ($excel _array); Delete the first array (title); $data = []; $i = 0; foreach ($excel _array as $k = + $v) {$data [$k] [' title '] = $v [1]; $data [$k] [' optiona '] = $v [2]; $data [$k] [' optionb '] = $v [3]; $data [$k] [' optionc '] = $v [4]; $data [$k] [' optiond '] = $v [5]; $data [$k] [' optione '] = $v [6]; $data [$k] [' optionf '] = $v [7]; $data [$k] [' time '] = Date (' y-m-d h:i:s ', Time ()); $data [$k] [' themeid '] = $themeid; $i + +; } $success =db::name (' Sleeptopic ')->insertall ($data); BULK INSERT data//$i = $error = $i-$success; echo "Total {$i}, success {$success} bar, failed {$error}. "; Db::name (' t_station ')->insertall ($city); BULK INSERT data}else{//upload failed get error message echo $file->geterror (); } }
Related recommendations:
TP5 Project Modification Database
TP5 Phpexcel Generate form instance sharing