Laravel MIME issue while validating .csv file

时光怂恿深爱的人放手 提交于 2020-05-23 08:26:26

问题


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 even if spending an hour behinding the silly thing.

this is my validation rule looks like.

public function rules()
{
    return [
       'sheet' => 'required',
       'file' => 'mimes:csv',
    ];
} 

However required is working file but on mimes.

I tried couple of other ways i founded like:

return [
    'sheet' => ['required', 'mimes:csv']
]

 

return [
    'sheet' => ['required', 'mimes:text/csv']
]

 

return [
    'sheet' => 'required|mimes:text/csv'
];

 

return [
    'sheet' => 'required|mimes:csv'
];

 

return [
    'sheet'          => 'required',
    'extension'      => 'required|in:csv'
];

Above none of line is working sometime says not valid file sometime passes through the validation.

I doubted for invalid file, then I downloaded fresh sample file from Microsoft site. That not the issue at all.

Can anyone help me?

Thanks!


回答1:


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'
];


来源:https://stackoverflow.com/questions/35570540/laravel-mime-issue-while-validating-csv-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!