multiupload file does not show error for big size only

hi there

i have these:

model:




class Item extends \yii\db\ActiveRecord

{

    public $files = array() ;

    ...

    ...

    ...

   public function rules()

    {

        return [

            ...

            ...

            ...

            [['files'], 'required'],

            [['files'], 'file', 'extensions' => ['jpg', 'png'], 'maxFiles' => 20],

        ];

    }

}



view:




...

...

<?= $form->field($model, 'files[]')->fileInput(['multiple' => true]);?>

...

...



it shows all errors like required and etc on the corresponding field except two below errors:

1- if i select one file with size more than limit size and submit, it refresh the page

2- if i select multiple files with size more than limit size and submit go to this error:

Please report it to issue tracker at github. Both cases should result in form errors (if there’s possibility to catch it).

hi samdark

thanks

i have this error too

am i wrong with sth?

Thought to shed some light over this one:




Bad Request (#400)

Unable to verify your data submission.



This is not Yii’s fault. This happens because you upload a file too large and the csrf token for yii simply does not have more room to pass, thus not getting validated at all. To fix this you should increase the max upload file size from your php.ini or to implement an ajax uploader that uploads in chunks.

i can save all my pictures when they are selected at the acceptable size

but on the save method i always get the above error

code from controller:




if($model->validate()){

                $model->category_id = $_GET['category_id'];

                $model->date = time();

                $model->files = UploadedFile::getInstances($model, 'files');

                foreach($model->files as $key => $value){

                    $model->files[$key]->saveAs('..\\..\\images\\' . $model->files[$key]->baseName . '.' . $model->files[$key]->extension);

                }

                $model->save(); #this line always has error

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



solved :)

i used another model for manage images

but yii must check it and show error at the corresponding field, as show for single file upload

controller:




public function actionCreate()

    {

        $model = new Item();

        $images = new Images();

        

        if(

            ($_GET['category_id'] < 1) ||

            !isset($_GET['category_id']) ||

            !is_numeric($_GET['category_id'])

        ) throw new NotFoundHttpException('Category ID is not defined or acceptable!');

        

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

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

                $images->images = UploadedFile::getInstances($images, 'images');

                if($model->validate() && $images->validate()){

                    foreach($images->images as $key => $value)

                        $images->images[$key]->saveAs('..\\..\\images\\' . $images->images[$key]->baseName . '.' . $images->images[$key]->extension);

                    

                    $model->category_id = $_GET['category_id'];

                    $model->date = time();

                    

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

                    else throw new NotFoundHttpException('Item can not be saved!');

                    

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

            }

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

    }



images model:




<?php

namespace common\models;


use yii\base\Model;

use yii\web\UploadedFile;


class Images extends Model

{

    public $images = array();


    public function rules(){

        return [

            [['images'], 'required'],

            [['images'], 'file', 'extensions' => ['jpg', 'png'], 'maxFiles' => 20],

        ];

    }

    

}



view:




...

...

<?= $form->field($images, 'images[]')->fileInput(['multiple' => true]);?>

...

...



thanks for your reply

I could be wrong but I think there’s no way to handle it properly in PHP.

thanks, do you mean i always will get following error?

Yes, something like that. The issue is that you’re getting POST alright but it’s trimmed. Doensn’t contain all necessary values and we can’t really say if it wasn’t submitter or it was trimmed because of the limit.

Hi. This error is related to PHP not Yii. This is because of server configuration. You should increase both post_max_size and upload_max_filesize (post_max_size should be more than upload_max_filesize). The posted values (containing files) are trimmed as mentioned and there is no way for Yii to determine which file is trimmed since it hasn’t the original files to compare and each data (such as size, mime type, etc.) are extracted from the uploaded file itself. Actually event PHP itself never can tell where the data is trimmed exactly.

Even if CSRF token is successfully passed, still you don’t have the files successfully uploaded. You can test it via a simple PHP form (without framework) yourself. The hidden constant (MAX_FILE_SIZE) in the form should not be greater than upload_max_filesize in php.ini and as you can see in this link there is a specific error code for php.ini config beside the hidden form element (MAX_FILE_SIZE).