yii2: Composite file extension validation is not supported
Feature request.
What steps will reproduce the problem?
Try to validate composite file extension such as tar.xz
What is the expected result?
File name file.bar.xz should not be uploaded only file.tar.xz should be allowed.
What do you get instead?
Any file ended with .xz is uploaded.
Additional info
Checkout this question on Stack Overflow
| Q | A |
|---|---|
| Yii version | 2.0.? |
| PHP version | 7 |
| Operating system |
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 18 (18 by maintainers)
Don’t fight the definition! As regarded in the Wikipedia, the file extension is the portion that is separated by period. It is so simple. Any thing after the first dot is regarded as an extension.
From the API, StringHelper::endsWith method takes two required string parameters and the third is optional, so we could use it like the following:
Again this is closest to be pseudo code!
But it’s not. For simple implementations it is better to check extension after the last dot instead of first one, and I can’t imagine any app which would decide that
com.indexis valid extension ofexample.com.htmlfile and does not treat it as a bug.@samdark Or we could use
StringHelper::endsWith()for comparing list of extensions with file path to check if file name match any of extensions specified inFileValidator::$extensions- then user can use anything here (evencom.html) and it still will be simpler and foolproof than regex. For MIME validation we may use “real” extension, because this is the only sane thing to do. So fortar.gzwe should check if file name ends with.tar.gzand for MIME validation we would check ifapplication/gziptype matchgzextension.I’ve re-checked everything.
'checkExtensionByMimeType' => false.'mimeTypes' => ['application/x-xz']if you’ll add corresponding mime type to config.pathinfoit is what comes after last.in the full name.In order to solve point 3, we can introduce another option to validate file name according to something like regular expression. Then you’d be able to do
'mask' => '~\.tar\.xz$~i'.