[[+content_image]]
H
H
Herman Martin2018-05-13 12:38:36
Yii
Herman Martin, 2018-05-13 12:38:36

How to upload files with arbitrary extensions in yii2?

I do as it is written here
the rules are

public function rules(){
        return [
          [['file'], 'file',
              'extensions' => 'png, jpg, jpeg, gif, docx, xlsx, pdf, djvu, rar, zip, 7zip,txt',
              'maxSize' => Yii::$app->params['fileMaxSize'],
              'skipOnEmpty' => false,
              'maxFiles' => 5,
              //'tooLarge'=>'File has to be smaller than 50MB'
              ],
            [ ['file'], 'required' ],
        ];
    }

so, he downloads pdf files, also txt, docs, but for example png or rar does not want, although they are listed in the extensions list, while he refers to the same list ...

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
H
Herman Martin, 2018-05-13
@dklight

I found a partial solution to the problem, if you look here, namely go to file and to 'checkExtensionByMimeType', and change the default value to false, then you get a solution to the problem, namely now you can upload files in rar format. Why exactly this format cannot be loaded without checking the conformity of the mime type is not clear.
The result is:

public function rules(){
    return [            
        ['file', 'file', 'extensions' => 'tar,png, jpg, jpeg, gif, docx, xlsx, pdf, rar, djvu, zip, 7z, txt,', 'maxSize' => Yii::$app->params['fileMaxSize'], 'maxFiles' => 5, 'checkExtensionByMimeType' => false],
        [ ['file'], 'required' ],
    ];
}

And partly because casting checkExtensionByMimeType to false may not always be good.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question