Как сохранять путь к загруженным файлам?
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
<?= $form->field($photo, 'photo[]')->widget(kartik\file\FileInput::className(), [
'options' => [ 'multiple' => true, 'accept' => 'image/*'],
'pluginOptions' => ['previewFileType' => 'image']
]);
?>
<?= Html::activeHiddenInput($photo, 'author', ['value' => Yii::$app->user->identity->username ]) ?>
<?= Html::activeHiddenInput($photo, 'role', ['value' => Yii::$app->user->identity->group ]) ?>
<?= Html::activeHiddenInput($photo, 'post_id', ['value' => $id]) ?>
<button>Submit</button>
<?php ActiveForm::end(); ?>
public function actionPhoto($id){
$photo = new Photo();
$model = $this->findModel($id);
if (Yii::$app->request->isPost) {
$photo->photo = UploadedFile::getInstances($photo, 'photo');
if ($photo->load(Yii::$app->request->post()) && $photo->save()) {
if ($photo->photo && $photo->validate()) {
foreach ($photo->photo as $ph) {
$ph->saveAs('uploads/' . $ph->baseName . '.' . $ph->extension);
}
}
return $this->redirect(['photo', 'id' => $model->id]);
} else {
return $this->render('photo', [
'model' => $model,
'photo' => $photo,
]);
}
}
}