控制器
public function actionCreate()
{
$model = new RoomTypes();
if ($model->load(Yii::$app->request->post())) {
// get the uploaded file instance. for multiple file uploads
// the following data will return an array
$image = UploadedFile::getInstance($model, 'image');
// store the source file name
$model->room_type = $image->name;
$imageName_1 = $model->room_type;
$ext = end((explode(".", $image->name)));
// generate a unique file name
$model->pic_1 = Yii::$app->security->generateRandomString().".{$ext}";
// the path to save file, you can set an uploadPath
// in Yii::$app->params (as used in example below)
$path = Yii::$app->params['uploadPath'] . $model->pic_1;
if($model->save()){
$image->saveAs($path);
return $this->redirect(['view', 'id'=>$model->_id]);
} else {
// error in saving model
}
}
return $this->render('create', [
'model'=>$model,
]);
}
型号:
public $image;
[['image'], 'safe'],
[['image'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg', 'maxFiles' => 4],
查看
<?= $form->field($model, 'room_type')->label(false)->textInput(['maxlength' => true]) ?>
<?php echo $form->field($model, 'image[]')->widget(FileInput::classname(), [
'options'=>['accept'=>'image/*', 'multiple'=>true],
'pluginOptions'=>['allowedFileExtensions'=>['jpg','gif','png']
'slugCallback' => new JsExpression(function(room_type)
{return room_type;},),]]);
?>
上传文件时出现此错误。 此外,它不接受多张图片上传,它有什么问题? 如何实现这个多文件上传???
答案 0 :(得分:-1)
你有错误印刷到模型规则:
[['$image'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg', 'maxFiles' => 4],
应该是
[['image'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg', 'maxFiles' => 4],