KHAN's avatar
Level 4

File MimeType Validation Returning Error Even With Correct MimeType

Hi,

I am uploading some files.

'attachments' => 'mimes:pdf,xsl,doc,docx,pptx,pps,jpeg,bmp,png',

I have set some rules for the mimetype here. However, even when i try to upload a pdf file. i still get the error

The attachments must be a file of type: pdf, xsl, doc, docx, pptx, pps, jpeg, bmp, png.
0 likes
7 replies
tomopongrac's avatar

Do you have in your form element

enctype="multipart/form-data"

KHAN's avatar
Level 4

@tomi

Yes i do.

{{Form::open(['route' => ['admin-event-store', $event->id], 'id' => 'store-event-form', 'files' => 'true'])}}

i inspected the element and also saw enctype="multipart/form-data"

This problem occur with my multiple file upload input

KHAN's avatar
Level 4

@tomi

Hi thank you, ive tried to implement that

<?php

namespace App\Http\Requests;

use App\Http\Requests\Request;
use Illuminate\Support\Facades\Auth;

class CreateEventRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return Auth::user()->role_id == 1;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rules = [
            'title' => 'required',
            'banner_image' => 'required|image|mimes:jpeg,bmp,png|max:5000',
            'attachments' => '',
        ];


        $attachments = $this->file( 'attachments' );

        if ( !empty( $attachments ) )
        {
            foreach ( $attachments as $key => $attachment ) // add individual rules to each image
            {
                $rules[ sprintf( 'attachments.%d', $key ) ] = 'mimes:pdf,xsl,doc,docx,pptx,pps|size:25000';
            }
        }

        return $rules;
    }
    /**
     * Get the error messages that should be displayed if validation fails.
     *
     * @return array
     */
    public function messages()
    {
        return [
            'hero_image.max' => 'The hero image cannot be greater than 5mb.',
        ];
    }
}

Ive tested and it doesent allow me to upload a zip file which is right. BUT it returns back to the form page with no error message. why are there no error messages

KHAN's avatar
Level 4

@rodrigo.pedra, @simik

Really struggling with this problem, if anyone could try doing the same thing to see the outcome, i would appreciate that a lot.

lindstrom's avatar

Ugh, I gave up on the mimes validation and just filtered on $request->file('import_file')->getClientOriginalExtension().

Please or to participate in this conversation.