I'm using yii\web\UploadedFile to upload excel and csv files following the documentation. Uploading works perfectly fine however, I've encountered a couple of weird issues which I can't figure out how to fix.
I have expressly stated on my rules to allow xls, xlsx and csv. ---> xls and xlsx are uploaded without any problems but csv files throw "Only files with these extensions are allowed: xlsx, xls and csv." error. I am 100% sure that I am uploading a csv file. Is there any specific reason this happens? How can it be fixed? For now, I've made a workaround by removing extensions on my rules and displaying a with error message when extension not xls, xlsx or csv but I'd much prefer it if I can use the default from the rules.
I mainly use files with japanese filenames. i.e.: テスト0001.xlsx and テスト0001.csv. When I try to echo or dump the filename generated on yii, I get the correct filename but when I check uploads folder, the filename changes to something like: 陬ス蜩√.xlsx. So far, I have tried using iconv to set charset to UTF8 with no luck. I've also tried hardcoding the filename just to check if it has something to do with the actual file but still same results. How can I retain the original filename when uploading the file?
Ad. 1. This is because there is checkExtensionByMimeType option in the FileValidator. It is set to true by default and it verifies if file extension matches the file MIME type. In case of csv files the MIME is usually text/plain which causes the validation error.
The solution is to set checkExtensionByMimeType to false for this kind of file uploads.
See the discussion about this.
Ad. 2. What version of Yii are you using? In Yii 2.0.10 the issue with wrong UTF-8 names encoding for file downloads has been fixed.
If this is not the case of old version or this issue has no effect on file names being saved maybe this is the problem with your version of ICU.
Check your ICU version by running the requirements.php from the main Yii 2 project folder and look for the ICU section.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With