Laravel MIME issue while validating .csv file

前端 未结 3 1699
感动是毒
感动是毒 2021-01-11 22:45

I have read couple of post regarding the issue of checking the file extension while upload a file using laravel.

I have the same issue and didn\'t file the solution

相关标签:
3条回答
  • 2021-01-11 23:16

    Here are several types I used for more complete csv validation, hope it will help future usage

            return [
                'sheet' => 'required|mimetypes:text/csv,text/plain,application/csv,text/comma-separated-values,text/anytext,application/octet-stream,application/txt'
            ];
    
    0 讨论(0)
  • 2021-01-11 23:29

    I faced the same issue, and even it is old I wish this will help the others. To fix it you must know what mime you got from your request, to find it use this line of code:

    dd($request->file('document')->getMimeType(),$request->file('document')->getClientOriginalExtension() );
    

    So in my case, the file that I uploaded is CSV but I got a txt file extension. So I solve it like the previous answers in this post by just adding txt to request validation:

    return [
        'sheet' => "required|mimes:csv,txt",
    ];
    
    0 讨论(0)
  • 2021-01-11 23:35

    Surprised!

    Its getting text/plain mime for the CSV, that was the cause of the issue.

    So to fix this I just found which extension is responsible for text/plain and I found txt so i just updated my rules:

    return [
        'sheet' => 'required|mimes:csv,txt'
    ];
    
    0 讨论(0)
提交回复
热议问题