<span style="font-size:18px;"><meta charset='utf8'>
<form action="?r=rikao/upload" method="post" enctype="multipart/form-data">
<input type="file" name="myfile">
<input type="submit" value="上传">
</form></span>
/*用TXT文件转化成csv格式,内容用逗号隔开*/
controller页面 引用yii2.0上传类use yii\web\UploadedFile;
//scv上传
public function actionUpload(){
header("content-type:text/html;charset=utf8");
$img=$_FILES['myfile']; //获取上传文件参数
//print_r($img);die;
$name= $img['name'];
$upload = new UploadedFile();
$upload->tempName=$img['tmp_name']; //设置上传的文件的临时名称
$img_path='public/'.$name; //设置上传文件的路径名称(这里的数据进行入库)
$arr=$upload->saveAs($img_path); //保存文件
//var_dump($arr);
$arrCSV = array();
// Open the CSV
if (($handle = fopen($img_path, "r")) !==FALSE) {
// Set the parent array key to 0
$key = 0;
// While there is data available loop through unlimited times (0) using separator (,)
while (($data = fgetcsv($handle, 0, ",")) !==FALSE) {
// Count the total keys in each row
$c = count($data);
//Populate the array
for ($x=0;$x<$c;$x++) $arrCSV[$key][$x] = $data[$x];
$key++;
} // end while
// Close the CSV file
fclose($handle);
} // end if
echo "<pre>"; print_r($arrCSV); echo "</pre>";
// return $this->render('file_list',['list'=>$arrCSV]);
}
view展示
<table border="1">
<tr>
<td>序号</td>
<td>用户名</td>
</tr>
<?php foreach ($list as $key => $value) { ?>
<tr>
<td><?php echo $value[0]?></td>
<td><?php echo $value[1]?></td>
</tr>
<?php } ?>
</table>