Php Yii2:尝试获取Yii2中非对象的属性
控制器Php Yii2:尝试获取Yii2中非对象的属性,php,yii,yii2,Php,Yii,Yii2,控制器 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
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;},),]]);
?>
上载文件时出现此错误它也不接受多张图片上传,有什么问题?
如何实现此多文件上载???您已将错误打印到模型规则中:
[['$image'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg', 'maxFiles' => 4],
应该是
[['image'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg', 'maxFiles' => 4],
不要创建另一个变量
$image
只需使用[['image'],'file','skipOnEmpty'=>false,'extensions'=>'png,jpg','maxFiles'=>4],并循环它们以存储多个图像。是否进行了更正,但仍然存在相同的错误:(您是否可以在控制器中在线显示错误?$model->room_type=$image->name;
您必须确保您的Activeform
具有用于多文件上载的选项。