Weird Behaviour Uploading CSV File

After failing in uploading a CSV using a model, I just copy and paste the model, view and controller that appears on the documentation (http://www.yiiframework.com/doc-2.0/guide-input-file-upload.html) and after checking that worked correctly I just edit it to accept other file formats.

What is weird is that in both cases the file is being uploaded correctly, but the "Validate()" method is failing for the CSV file.

Here you can see both the Object uploaded before and after the validation.

The validation result is 1 for the picture and empty for the CSV.

First for a picture :


app\models\UploadForm Object

(

    [imageFile] => yii\web\UploadedFile Object

        (

            [name] => CAJITA.jpg

            [tempName] => /tmp/phpz2ER1z

            [type] => image/jpeg

            [size] => 161126

            [error] => 0

        )


    [_errors:yii\base\Model:private] => 

    [_validators:yii\base\Model:private] => 

    [_scenario:yii\base\Model:private] => default

    [_events:yii\base\Component:private] => Array

        (

        )


    [_behaviors:yii\base\Component:private] => 

)


yii\web\UploadedFile Object

(

    [name] => CAJITA.jpg

    [tempName] => /tmp/phpz2ER1z

    [type] => image/jpeg

    [size] => 161126

    [error] => 0

)



For a CSV file:




app\models\UploadForm Object

(

    [imageFile] => yii\web\UploadedFile Object

        (

            [name] => Comercial y Marketing_TRIMMED.csv

            [tempName] => /tmp/phpei4csl

            [type] => text/csv

            [size] => 2913

            [error] => 0

        )


    [_errors:yii\base\Model:private] => 

    [_validators:yii\base\Model:private] => 

    [_scenario:yii\base\Model:private] => default

    [_events:yii\base\Component:private] => Array

        (

        )


    [_behaviors:yii\base\Component:private] => 

)


yii\web\UploadedFile Object

(

    [name] => Comercial y Marketing_TRIMMED.csv

    [tempName] => /tmp/phpei4csl

    [type] => text/csv

    [size] => 2913

    [error] => 0

)



Anyone with this issue ?

I just realized that the error is that "CSV" is not allowed, but it is.

In the form to upload it realize that is a csv and it can be uploaded, but in the validation process it says that is not allow because is a csv, while this extension is already allowed.

Now you can see the object and the validation output.


yii\web\UploadedFile Object

(

    [name] => TRIMMED.csv

    [tempName] => /tmp/phpXvhaIe

    [type] => text/csv

    [size] => 2913

    [error] => 0

)

Array

(

    [imageFile] => Array

        (

            [0] => Only files with these extensions are allowed: png, jpg, csv, pdf.

        )


)

It seems that there is a "bug" that affects some files at validating the mime type.

In my case it working disbling the mime type validation in the rules of the model.





    public function rules() {

        return [

            [

                ['imageFile'], 'file', 'skipOnEmpty' => false, 

                'extensions' => 'csv',

                'checkExtensionByMimeType' => false,

            ],

        ];

    }




This way I am uploading the CSV files. I recommend you to do this only if you are having troubles, mime validation is really useful in case that someone wants to harm you.

What was your validation rule before?

The same without ‘checkExtensionByMimeType’ => false,

There was issue with this https://github.com/yiisoft/yii2/issues/6148

when I add the ‘checkExtensionByMimeType’ property I get an error saying the property is unknown