I have a form and I’m trying to load image to DB and to the filesystem like this.
Controller
public function actionCreate()
{
$model = new Page();
if ($model->load(Yii::$app->request->post()))
{
$imageName = $model->url;
$model->file = UploadedFile::getInstance($model, 'file');
$model->image = 'avatars/'.$imageName.'.'.$model->file->extension;
if ($model->validate() && $model->save())
{
$model->file->saveAs('avatars/'.$imageName.'.'.$model->file->extension);
return $this->redirect('page/'.$model->url);
}
}
return $this->render('create', ['model' => $model]);
}
Model
//it contains only rules
public function rules()
{
return [
[['name', 'position', 'description', 'contact', 'url', 'user_id', 'type', 'layout', 'file'], 'required'],
[['description'], 'string', 'min' => 10, 'max' => '255'],
[['user_id'], 'integer'],
[['name', 'position'], 'string', 'length' => [5, 32]],
[['contact'], 'url', 'defaultScheme' => 'http'],
[['file'], 'image', 'extensions' => ['jpg', 'jpeg', 'png'], 'minSize' => 100*100, 'maxSize' => 1000*1000, 'skipOnEmpty' => false],
[['image'], 'string'],
[['url'], 'string', 'length' => [5, 12]],
//[['url'], 'unique'],
];
}
But instead of loading the success page, Yii renders the form and says, that file isn’t set, even if it is. What is the problem?