Diglett's avatar

Laravel validation mime type errors

So I am trying to make an upload form that validates images, .jpg, .png, .gif.

if (Input::hasFile('files')) {
    $files = Input::file('files');

    foreach($files as $file) {

        $validator = Validator::make(
            array('file' => $file),
            array('file' => 'required|mimes:jpeg,png,gif|image|max:5000')
        );

        if ($validator - > passes()) {
            // passes
        } else {
            // fails
        }
...

This seems to work fine for .jpg, .png and animated gifs, but when uploading a .gif that doesn't animate the validator fails. Not sure what's going on.

Just started using Laravel, any information would be appreciated.

0 likes
7 replies
Diglett's avatar

It returns: The MIME type for your file is: image/gif

phildawson's avatar

That is odd. What does the error message show on the page?

        if ($validator -> passes()) {
            // passes
        } else {
            dd($validator->errors()->all()); 
        }

Btw the image in the validation rule is an alias for mimes:jpeg,png,gif,bmp,svg so unless you are after bmp,svg it isn't needed or if they are then you can remove mimes:jpeg,png,gif

Diglett's avatar

Thanks, I changed the image validation rule.

Anyways, it returns:

array:1 [
  0 => "The files must be a file of type: jpeg, png, gif."
]

And still, only for images that don't animate. Images that animate will work just fine for some reason.

Diglett's avatar

Okay, so apparently it returns bin as guessed extension, which means it failed to guess it:

[
{
error: "The files must be a file of type: jpeg, png, gif.",
mime: "image/gif",
file: "noanimation.gif",
guessExtension: "bin"
}
]

Any way to prevent this? I should probably just remove this validation completely as it is returning unexpected results and create my own validation.

Please or to participate in this conversation.