Uploading file help please


I have found this tutorial about uploading a single file which is what I am trying to do:

There is one thing I do not yet understand, they say to create a model and controller but what if this upload field is part of another form which runs with another controller and model?

<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?>

<?= $form->field($model, 'file')->fileInput() ?>

Let’s say I have this form:

    <?php $form = ActiveForm::begin(); ?>

    <?= $form->field($model, 'title')->textInput(['maxlength' => 160]) ?>

    <?= $form->field($model, 'posts')->textarea(['rows' => 6]) ?>

    <?= $form->field($model, 'keywords')->textInput(['maxlength' => 45]) ?>

    <?= $form->field($model, 'categories_id')->dropDownList(


    ['prompt'=>'Select a category']) ?>



    <?= $form->field($model, 'user_id')->textInput(['value' => \Yii::$app->user->identity->id]) ?>



    <div class="form-group">

        <?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>


    <?php ActiveForm::end(); ?>

How to put this upload data into it?

Finally, what I do not understand is how to retrieve the file path and where will the file be recorded in the database if I wish to output the picture in another view?

Thank you,


The Link You have is Too enough for file Uploading, simply use that one, for other models use that model name , and use getinstance of that model from the controller, save image name in db, and file in filepath (eg./images/yourimagename.ext),

for view , use basePath and image tags.

Thank you for the reply.

Do you know a good tutorial somewhere please as I am still new to Yii2.

I cannot find anything that shows a step by step on the Yii website.

Thank you,


In your form you just have to add a file input and ‘enctype’ => ‘multipart/form-data’ to the form widget options.

<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?>

//with model

<?= $form->field($model, 'file')->fileInput() ?>

//without model

<?= yii\helpers\Html::fileInput('file-name'); ?>

Now on your controller you take the file like this:

if (Yii::$app->request->isPost) {

    //with model

    $model->file = UploadedFile::getInstance($model, 'file');

    $model->file->saveAs('uploads/' . $model->file->baseName . '.' . $model->file->extension)

    //without model

    $file = UploadedFile::getInstanceByName('file-name');

    $file->saveAs('uploads/' . $file->baseName . '.' . $file->extension)


And that’s all I need really?

No name space at the top or helpers or anything similar?

When you said in the the controller, do you mean in the actionCreate()?

    public function actionCreate()


        $model = new Posts();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {

            return $this->redirect(['view', 'id' => $model->id]);

        } else {

            return $this->render('create', [

                'model' => $model,




Thank you,


That’s all you need.

Of course, you will need all the namespaces, I didn’t mention that because is obvious.

And yes, when I said in your controller I meant in your actionCreate() or whatever is called.

Thanks Alexint,

I have a similar code to yours but keep getting an error as stated on this thread, any idea what is wrong please?