MrMooky's avatar

File validation not working correctly

I'm not sure if I'm doing something wrong, or if this is a bug. Right now, my validator looks like this (the line breaks look off in the preview):

$validator = Validator::make($request->all(), [
            'attachments.*' => 'mimes:png,jpg,jpeg,svg,csv,txt,xlx,xls,xlsx,pdf,doc,docx,zip,rar,wav,mp3,mp4,mov,mkv,webm,avi,ogg,m4a,flac,wma,aac,sketch,psd|max:500000',
]);

It basically works. So when I upload for example a xml file, $validator->fails() returns true.

But when I upload a html file, the file is being uploaded without errors and $validator->fails() returns false. And when uploading a wav file, which is allowed, it fails.

alt text

What am I doing wrong?

0 likes
9 replies
karam mustafa's avatar

Hello, I Think this file consider as a text Look Common MIME types

MrMooky's avatar

@karam mustafa Ah ok, that solves the .html question. Thanks. When allowing .txt, html is allowed as well. Any idea why .wav is not working even though it is allowed? I haven't checked stuff like sketch or psd yet, but I feel like I'm getting similar, unclear results.

Sinnbeck's avatar

If you have a linux based system, you can check the mime type on the commandline

file --mime-type -b filename.wav
MrMooky's avatar

@Sinnbeck Hm, nothing really something I can work with:

file --mime-type -b eagle-has-landed.wav

gives me inode/x-empty

file --mime-type -b alpinejs.min.js

gives me text/plain

But again, doesn't seem right. If that would be the case, then the validator does not make sense the way it is documented. As seen in my initial post.

As per docs: https://laravel.com/docs/8.x/validation#rule-mimes

The file under validation must have a MIME type corresponding to one of the listed extensions.

So the mimes rule should apply to extensions, not the actual mime type: https://laravel.com/docs/8.x/validation#rule-mimetypes.

Sinnbeck's avatar

@MrMooky can you actually play that file? eagle-has-landed.wav

I believe inode/x-empty is an empty file

MrMooky's avatar

@Sinnbeck Ah sorry, my bad. ._. audio/x-wav it is. And uploading that file works as expected. Damn it. :D

Sinnbeck's avatar

@MrMooky great to hear. It's a good test to check your files. Makes it easy to spot weird mimes (js being txt)

1 like

Please or to participate in this conversation.