Template page/index.html
<span style="color:#000000;"><meta charset='utf-8'></meta> <form action="{:U('Index/uploads')}" method="post" enctype="multipart/form-data"> 上传<select onchange='aa()' id='sel'><?php for($i=1;$i<=10 ;$i++ ){?> <option value=<?php echo $i;?>><?php echo $i;?></option><?php }?></select>个<p> <div id="div" class=""> <input type="file" name="file"><p> </div><input type="submit" value="提交" /> </form> <script type="text/javascript"> function aa(){ var sel=document.getElementById('sel').value; var str=''; for (var i=0;i<=sel-1 ;i++ ) { str+='<input type="file" name="file'+i+'"><p>'; } document.getElementById('div').innerHTML=str; } </script></span>
public function uploads(){
$upload = new ThinkUpload(); // Instantiate the upload class
$upload->maxSize = 3145728 ; // Set the attachment upload size
$upload ->exts = array('jpg', 'gif', 'png', 'jpeg','xls');//Set attachment upload type
// Upload files
$info = $upload->upload( );
if(!$info){//Upload error message
$this->error($upload->getError());
}else{//Upload successful
foreach($info as $ file){
$files='./Public/'.$file['savepath'].$file['savename'];
vendor("Excel.PHPExcel");
vendor("Excel.PHPExcel.PHPExcel_IOFactory" );
$PHPExcel = new PHPExcel();
//var_dump($PHPExcel);
$PHPReader = new PHPExcel_IOFactory();
//var_dump($PHPReader);die;
$xlsPath =$files; //Specify Exls path to be read
//$type = 'Excel2007'; //Set the Excel type to parse Excel5 (2003 or below) or Excel2007
//echo $xlsPath;
$type = 'Excel5';
$ xlsReader = $PHPReader->createReader($type);
$xlsReader->setReadDataOnly(true);
$xlsReader->setLoadSheetsOnly(true);
$Sheets = $xlsReader->load($xlsPath);
//Start reading a
$Sheet = $Sheets->getSheet(0)->toArray(); //Read the first worksheet (note that the number starts from 0) You can do this if you read multiple A loop of 0,1,2,3....
//Get a two-dimensional array. Each small array is a row of the excel table content, which contains the data of each column of this row
//echo '' ;
//print_r($Sheet);
foreach($Sheet as $k=>$v){
$model = M('student');// Get successfully uploaded file information
$data['name ']=$v[0];
$result=$model->add($data);
if($result){
$this->success("Successful storage",U('Index/ excellist'));
}else{
$this->error("Storage failed");
}
}
}
}
}
The above introduces the example of uploading multiple excel files and importing into the database in tp3.2, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.