本文通过实例代码给大家介绍了Yii2.0多文件上传实例说明,非常不错,具有参考借鉴价值,需要的朋友参考下吧
<p style="text-align: center">
<span style="font-size: 24px; font-family: Simsun; white-space: normal; word-spacing: 0px; text-transform: none; float: none; font-weight: bold; color: rgb(0,0); font-style: normal; orphans: 2; widows: 2; display: inline !important; letter-spacing: normal; text-indent: 0px; font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px"><span class="Apple-converted-space">
创建控制器FormController
renderPartial('myfiles',['model'=>$model]);
}
public function actionGetfiles(){
$model = new Uploadm();
if (Yii::$app->request->isPost) {
$model->imgFile = UploadedFile::getInstances($model,'imgFile');
if ($model->upload()) {
// 文件上传成功
echo '上传成功';
}
}
}
创建模型Uploadm.PHP
5],//最多5张
];
}
public function upload()
{
if ($this->validate()) {
foreach ($this->imgFile as $file) {
$file->saveAs('uploads/' . $file->baseName . '.' . $file->extension);
}
return true;
} else {
return false;
}
}
}
创建视图/views/form/myfiles.PHP
'login-form','options' => ['class' => 'form-horizontal','enctype' => 'multipart/form-data'],'action'=>'?r=form/getfiles','method'=>'post'
]) ?>
= $form->field($model,'imgFile[]')->fileInput(['multiple' => true]) ?>
<div class="form-group">
<div class="col-lg-offset-1 col-lg-11">
<?= Html::submitButton('上传',['class' => 'btn btn-primary']) ?>