Call to a member function saveAs() on a non-object

Hi all, i’m following this guide to try upload file funcition.

But when i press Submit, i have this error:

 PHP Fatal Error – yii\base\ErrorException

Call to a member function saveAs() on a non-object

This is my controller


use app\models\admin\UploadForm;

use yii\web\UploadedFile;

        $upform = new UploadForm();

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

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

            if ($upform->validate()) {                

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





this is the view:

    use yii\helpers\Html;

    use yii\widgets\ActiveForm;


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

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


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

and the simply model:

namespace app\models\admin;

use yii\base\Model;

use yii\web\UploadedFile;


 * UploadForm is the model behind the upload form.


class UploadForm extends Model



     * @var UploadedFile|Null file attribute


    public $file;


     * @return array the validation rules.


    public function rules()


        return [

            [['file'], 'file'],




I don’t know what i’m missing,

Thank’s for any help!

Have you tried debugging your code?

would mean that you aren’t getting the file correctly and that your issue is probably with:

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

I would suggest var dumping $upform->fil, see what you get and go from there



thank’s Ash for your reply.

I found the problem:

The controller i’m using is my ProfileController and i render the Profile view (where i create upload form) passing these parameter:

        return $this->render("profile", [

            'profile' => $profile, // profile model

            'upform' => $upform, //form model

            'nav' => $nav, // navigation wizard step



then, on my Profile View i have another render to sub-view:

    switch($nav) {

            case "1": $navpage = '_profile_general'; break;

            case "2": $navpage = '_profile_contact'; break;

            case "3": $navpage = '_profile_social'; break;

            default: $navpage = '_profile_general'; break;


        echo $this->render($navpage,[

                                        'profile' => $profile,

                                        'upform' => $upform


probably the command

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

return null and i’m watching is that $_FILES is empty :(

If I try to put the upload form in the main Profile View, all works fine, but if i put it in the sub_view, in the controller i have $_FILES empty.

I don’know why.