Genaut
Genaut

Reputation: 1851

Yii2 Upload image file dont save the file, but I see the post data OK

I have the next form:

$form = ActiveForm::begin(['id' => 'form-profile-edit', 'options' => ['enctype' => 'multipart/form-data']]); ?>
     <?= Html::activeFileInput($model, 'image_upload', ['class' => 'hidden', 'id' => 'file-avatar', 'accept' => 'image/*', 'onchange' => 'javascript:this.form.submit()'])?>
<?php ActiveForm::end();

This rules in the model:

['image-upload', 'file', 'extensions' => ['png', 'jpg', 'jpeg'], 'maxSize' => 45000],
[['image-upload'], 'safe'],

And this code in the controller:

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

            $file = \yii\web\UploadedFile::getInstance($model, 'image_upload');
            if ($file) {
                //print_r($_FILES);
                echo "<br>pathactual: " . getcwd();
                echo "<br>basename: " . $file->baseName;
                echo "<br>extension: " . $file->extension;
                echo "<br>name: " . $file->name;
                echo "<br>SaveUrl: " . Yii::$app->homeUrl . "assets/avatars/" . $file->baseName . "." . $file->extension;
                echo "<br>Error: " . $file->error; //This shows 0
                echo "Size: " . $file->size;

                $model->avatar = $file->baseName. "-big." . $file->extension;

                if($model->update(true, ["avatar"])){
                    echo "<br>avatar actualizado";
                    Yii::$app->getSession()->setFlash('profile-Msg-OK', Yii::t("app", "Generic_Changes_OK"));
                    //Guardamos el fichero del avatar
                    if($file->saveAs(Yii::$app->homeUrl . "assets/avatars/" . $file->baseName . "-big." . $file->extension)){
                        echo "<br>Imagen guardada correctamente.";
                    }else{
                        echo "<br>Imagen no guardada correctamente: " . $file->error; //This shows 1

                    }
             }

}

I can see the $file object correct, size, tempName, baseName, extension.. I save the avatar name in $model->avatar and then I make the update. This works fine.

Now, I execute the saveAs method but always enters in else, the $file->errror value in this moment is 1. I see the UploadedFile documentation and see:

$error integer An error code describing the status of this file uploading.

Search the possible status here and I see that 1 corresponds to:

UPLOAD_ERR_INI_SIZE Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.

Then, I go to my php.ini file (I revised it from phpinfo) I have 40M

upload_max_filesize 40M 40M

The files what I am using to test have 14 and 16 KB. Any idea about this?

If anybody need more info, please comment

Upvotes: 1

Views: 1648

Answers (1)

Tony
Tony

Reputation: 5867

You need to correct the saving path

if($file->saveAs(Yii::$app->homeUrl . "assets/avatars/" . $file->baseName . "-big." . $file->extension))

here

Yii::$app->homeUrl

is not what you want ,because its a url of your application, you need something like

if($file->saveAs(Yii::getAlias('@webroot') . "/assets/avatars/" . $file->baseName . "-big." . $file->extension))

an absolute path in filesystem

Upvotes: 1

Related Questions