FileValidator does not appear to validate extensions

Hi all,

I’ve created a model to upload CSV files, thus:


<?php


namespace app\models;


use yii\base\Model;

use yii\web\UploadedFile;


/**

 * UploadForm is the model behind the upload form.

 */

class UploadFile extends Model

{

    /**

     * @var UploadedFile file attribute

     */

    public $file;


    /**

     * Check that the uploaded file is a CVS file

     * @return array the validation rules.

     */

    public function rules()

    {

        return [

            [['file'], 'file', 'skipOnEmpty' => false, 'extensions' => 'csv, txt',],

        ];

    }

}


?>

I then created a controller and view to allow a CSV file to be uploaded. If I specify an extensions rule and then try to upload a CSV file, I get the following error message:

yet the file I uploaded definitely has .csv extension.

Is this a bug in the Yii2 FileValidator class, or am I doing something wrong here?

I inserted the following logging code into the FileValidator::validateExtension method:


Yii::info("In validateExtension, extension = $extension, valid extensions = " . var_dump($this->extensions));

and when I looked at the log file, the message logged was as follows:

I have a form to upload files and works well.

I’m using the example that is in the documents (modified slightly).


<?php


namespace app\models;


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', 'extensions' => 'jpg, png, gif', 'skipOnEmpty' => false],

        ];

    }

    

    public function attributeLabels()

    {

        return ['file' => 'Archivo'];

    }

}

in FileValidator.php,

/**


 * @var boolean whether to check file type (extension) with mime-type. If extension produced by


 * file mime-type check differs from uploaded file extension, the file will be considered as invalid.


 */


public &#036;checkExtensionByMimeType = true;

So, if the mimetype of the uploaded file is different from the extension, you will get errors.