yii2 file upload error

upload image in yii2 advanced
yii2 upload file to database
yii2 upload file without model
yii2 ajax file upload
yii2 file type
yii2 $_files
yii2 rest api file upload
upload video file in yii2

i've learned a little bit about yii2 framework, but i got an error wich i cant solve..

I'm trying to add a image to signup form.

view:

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

            <?= $form->field($model, 'voornaam')->textInput(['autofocus' => true]) ?>
            <?= $form->field($model, 'bedrijf') ?>
            <?= $form->field($model, 'telefoon') ?>
            <?= $form->field($model, 'username')?>
            <?= $form->field($model, 'email') ?>
            <?= $form->field($model, 'password')->passwordInput() ?>
            <?= $form->field($model, 'file')->fileInput() ?>

            <div class="form-group">
                <?= Html::submitButton('Signup', ['class' => 'btn btn-primary', 'name' => 'signup-button']) ?>
            </div>

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

controller

public function actionSignup()
{
    $model = new SignupForm();
    if ($model->load(Yii::$app->request->post())) {
        if ($user = $model->signup()) {
            if (Yii::$app->getUser()->login($user)) {
                return $this->goHome();
            }
        }
    }

    return $this->render('signup', [
        'model' => $model,
    ]);
}

model:

public function signup()
{
    if (!$this->validate()) {
        return null;
    }

    $user = new User();

            $imageName = $user->username;
            $user->file = UploadedFile::getInstance($user,'file');
            $user->file->saveAs( 'uploads/'.$imageName.'.'.$model->file->extension );
            $user->picture =  'uploads/'.$imageName.'.'.$model->file->extension; 

    $user->voornaam = $this->voornaam;
    $user->bedrijf = $this->bedrijf;
    $user->telefoon = $this->telefoon;
    $user->username = $this->username;
    $user->email = $this->email;
    $user->setPassword($this->password);
    $user->generateAuthKey();

    return $user->save() ? $user : null;
}

i'm getting the error: Call to a member function saveAs() on null

What did i do wrong? (I'm using the advanced template).

Thanks.

you should use $this instead of $user or you should add

$user=$this

because simply $user is new instance and it's field are empty !

file upload failed - General Discussions, Hi Guys, hope you all always in good health. short story this is basic knowledge that i don't know. i have created an upload function but it  Uploading Files. Uploading files in Yii is done via a form model, its validation rules and some controller code. Let's review what's required to handle uploads properly. Uploading single file. First of all, you need to create a model that will handle file uploads. Create models/UploadForm.php with the following content:

It looks like you are mentioning wrong file path, you must mention rootpath for save function for example

   $user->file->saveAs(\Yii::getAlias('@webroot').'/uploads/'.$imageName.'.'.$model->file->extension );

file-upload-error-validator | Extensions, file-upload-error-validator Validates PHP error status for an uploaded file. Requirements; Usage. Note: This validator is redundant with  Now, i use default Yii2 upload file. I still using yii2basic application that created before. If you haven't develop that, please prefer this link to develop. First, plase running this query or add fields foto to your mahasiswa table : alter table mahasiswa add foto varchar(50); Then, create uploads direcoty on yii2basic/web for save the file

I used separate upload function to upload a file and it's worked.

my view

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

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

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

            <?= $form->field($model, 'password')->passwordInput() ?>

            <?= $form->field($model, "files")->fileInput() ?>

            <div class="form-group">
                <?= Html::submitButton('Signup', ['class' => 'btn btn-primary', 'name' => 'signup-button']) ?>
            </div>

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

controller

public function actionSignup() {
        $model = new SignupForm();
        if ($model->load(Yii::$app->request->post())) {
            $model->files = \yii\web\UploadedFile::getInstance($model,'files');

            if ($model->upload() && $user = $model->signup()) {
                if (Yii::$app->getUser()->login($user)) {
                    return $this->goHome();
                }
            }
        }

        return $this->render('signup', [
                    'model' => $model,
        ]);
    }

and my model is

//declare variable
public $files; 

//add this to your rules
[['files'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg, jpeg']


//File upload function
public function upload()
    {
        if ($this->validate()) {
            $this->files->saveAs('../web/uploads/' . $this->files->baseName. '.' .$this->files->extension);
            $this->picture = '../web/uploads/' . $this->files->baseName. '.' .$this->files->extension;
            return true;
        } else {
            return false; 
        }
    }


    public function signup()
    {
            $user = new User();
            $user->username = $this->username;
            $user->email = $this->email;  
            $user->picture = $this->picture;
            $user->setPassword($this->password);
            $user->generateAuthKey();
            if ($user->save()) {
                return $user;
            }
            else{
                return null;
            }

    }

UploadedFile, yii\web\UploadedFile, $error public property. An error code describing the status of this file uploading. See also https://secure.php.net/manual/en/features.file-upload.errors.php. yii\web\UploadedFile: getHasError() yii\web\UploadedFile: getInstance() Returns an uploaded file for the given model attribute. yii\web\UploadedFile: getInstanceByName() Returns an uploaded file according to the given file input name. yii\web\UploadedFile: getInstances() Returns all uploaded files for the given model attribute. yii\web\UploadedFile

Just sharing you my solution, change the path to uploads folder to ../uploads/

In your model try changing saveAs function as shown below

$user->file->saveAs( '../uploads/'.$imageName.'.'.$model->file->extension );

I am a beginner in yii2, I faced a problem when i tried the file upload code from https://www.yiiframework.com/doc/guide/2.0/en/input-file-upload,

this solved my problem

Input file upload, Uploading files in Yii is done via a form model, its validation rules and some if (!​$file instanceof UploadedFile || $file->error == UPLOAD_ERR_NO_FILE)  BlueImp File Upload Widget for Yii2. Renders a BlueImp jQuery File Upload plugin. That plugin integrates multiple file selection, drag&drop support, progress bars, validation and preview of images. Installation. The preferred way to install this extension is through composer. Either run

yii2 file upload error - yii2 - iOS, i've learned a little bit about yii2 framework, but i got an error wich i cant solve.. I'​m trying to add a image to signup form. view: <?php $form  Uploading files in Yii is usually done with the help of yii\web\UploadedFile which encapsulates each uploaded file as an UploadedFile object. Combined with yii\widgets\ActiveForm and models, you can easily implement a secure file uploading mechanism.

Error Messages Explained - Manual, ini . UPLOAD_ERR_FORM_SIZE. Value: 2; The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form. Check the filesize you are trying to upload and make sure it is less than the value of upload_max_filesize in you php.ini. You can check that value on your PHP Info as well - whatever is easier. If that is your problem, assuming you have access to make changes to the php.ini file, just increase that value to what you need or want.

ver 1.0.4 Error · Issue #65 · 2amigos/yii2-file-upload-widget · GitHub, I think the error code in the updated plugin Jquery File Upload /* view */ use dosamigos\fileupload\FileUploadUI; use yii\helpers\Url; use  When submitting large files, changed settings in php.ini, should include not only upload_max_filesize, but also post_max_size. Many developers forgets about second parameter (as this is mentioned in PHP File Uploads Common Pitfalls).

Comments
  • Thanks, this worked! But it gives an next error: Calling unknown method: yii\web\UploadedFile::save() I'm using the vendor extension from yii itself use yii\web\UploadedFile; My save line: $this->file->save();
  • i think you should use saveAs();
  • I managed to get it working, What i did was, the last line from $this to $user, at the end it save the register for user. $user->picture = 'uploads/'.$imageName.'.'.$this->file->extension;
  • i replaced the line with yours, and made an uploads folder in the root, but still didnt work and same error..