user1597438
user1597438

Reputation: 2221

Yii2: Setting rules for csv file upload and retaining original filename

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.

  1. 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.

  2. 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?

Upvotes: 4

Views: 2814

Answers (2)

Ibrahim.H
Ibrahim.H

Reputation: 1195

I've spent some time with similar issue, only the mimeType for xlsx is not validated on the backend even when setting: 'mimeTypes'=>'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' I had to add a double check only for xlsx file type as follows:

    public function rules()
    {
        return [
            [['file'], 'file', //'extensions' => 'xlsx,xls',
                'mimeTypes'  => [
                    'application/vnd.ms-excel',
                    'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
                    ],
                'wrongMimeType'=> \Yii::t('app','Only excel files are allowed.'),
                'checkExtensionByMimeType' => false,
                'skipOnEmpty' => false],
        ];
    }

    public function upload() {
         if ($this->file && ($this->validate() || $this->file->type=='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')) {
            $this->file->saveAs('uploads/' . $this->file->baseName . '.' .
               $this->file->extension);
            return true;
         } else {
            return false;
         }
      }

The full example found here: https://yii2-framework.readthedocs.io/en/stable/guide/input-file-upload/

Hope this helps.

Upvotes: 2

Bizley
Bizley

Reputation: 18021

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.

Upvotes: 6

Related Questions