rabol's avatar
Level 14

Help with mime type validation for file upload

Hi

I'm trying to add mime type validation to a file upload so that it will only accept dotx (Word template files)

The validation code looks like this

$req->validate([
            'file' => 'required|mimetypes:application/vnd.openxmlformats-officedocument.wordprocessingml.template|mimes:dotx|max:2048'
        ]);

Validation fails

the 'dump' of the file in the request looks like this:

"_token" => "k1lVpV6EOUfbmHPPIdDpQJy1rIrOSgJ3Gxj7FkWZ"
  "submit" => null
  "file" => Illuminate\Http\UploadedFile {#419 ▼
    -test: false
    -originalName: "esd_test_1.dotx"
    -mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.template"
    -error: 0
    #hashName: null
    path: "/tmp"
    filename: "phpyUp9CW"
    basename: "phpyUp9CW"
    pathname: "/tmp/phpyUp9CW"
    extension: ""
    realPath: "/tmp/phpyUp9CW"
    aTime: 2020-09-22 08:42:08
    mTime: 2020-09-22 08:42:08
    cTime: 2020-09-22 08:42:08
    inode: 1048607
    size: 12006
    perms: 0100600
    owner: 1000
    group: 1000
    type: "file"
    writable: true
    readable: true
    executable: false
    file: true
    dir: false
    link: false
  }

what did I miss?

0 likes
4 replies
automica's avatar

@rabol isn't this failing on its size?

your file is:

size: 12006

and you have

max:2048
rabol's avatar
Level 14

If i understand it correctly, the 2048 in the validation is KB so 2048kb the file is 12006 bytes

and the validation error is:

The file must be a file of type: application/vnd.openxmlformats-officedocument.wordprocessingml.template.
The file must be a file of type: dotx.
rabol's avatar
Level 14

Issue is actually that the php extension fileinfo returns a different mime type. The mime type returned is actually: application/vnd.openxmlformats-officedocument.wordprocessingml.document

Please or to participate in this conversation.