S
S
Stepan2015-02-03 18:45:31
Yii
Stepan, 2015-02-03 18:45:31

Yii2 how to save photo path in form?

How to save path to downloaded files?

<?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,
                ]);
            }
        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Semenko, 2015-02-03
@abler98

No way, only the user has the right to set the value in the file field.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question