Laravel MIME issue while validating .csv file
Asked Answered
B

5

21

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!

Bedlamite answered 23/2, 2016 at 6:39 Comment(2)
Try this: application/csv.Doall
No luck! I posted answer. That is working for me!Bedlamite
B
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'
];
Bedlamite answered 23/2, 2016 at 9:9 Comment(4)
I add an .sql file and apply your validate rule but its still pass.Donnenfeld
But why this is happening, when I dumped the file variable it shows mimeType: "text/csv"Chainplate
This answer is not correct. Because user can upload txt files too!Sidwohl
CSV is in fact text. Only a different extension.Equatorial
G
7

I faced the same issue, and even if it was old, I wish that this will help the others. To fix this, you have to 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 was CSV but I got a txt file extension. So I solved it like the previous answers in this post by just adding txt to request validation:

return [
    'sheet' => "required|mimes:csv,txt",
];
Gilliam answered 2/1, 2021 at 22:16 Comment(0)
C
4

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'
        ];
Capelin answered 25/6, 2020 at 22:2 Comment(0)
A
0

detection of csv mime type requires several lines of text with consistent row format (probably the same number of commas per line) otherwise the file is indistinguishable from a plain text file.

Amar answered 18/4 at 17:54 Comment(0)
N
0

To resolve this issue, ensure the PHP Fileinfo extension is enabled. You can do this by uncommenting the extension=fileinfo line in your php.ini file and restarting your web server.

Good luck!

Nolan answered 1/8 at 9:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.