sebasti4n's avatar

"The file field is required" but it is present in $request->allFiles()?

I have a file upload form with enctype="multipart/form-data" that has this field:

<input type="file" name="file" id="file" required accept=".csv,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel" />

When uploading, I have the following validation rule:

'file' => 'required|mimetypes:'.implode(',', [
                    'text/csv',
                    'text/plain',
                    'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
                    'application/vnd.ms-excel',
                    'application/octet-stream',
                ]),

However, it fails with the error "The file field is required", but when I dd($request->allFiles()) there indeed is an array item called "file" which references an Illuminate\Http\UploadedFile instance.

What am I missing?

0 likes
11 replies
DhPandya's avatar

@sebasti4n Could you please add your view code of the form only? Make sure your form tag has enctype="multipart/form-data" property included. Well are you submitting your form using AJAX ?

sebasti4n's avatar

@DhPandya

<form action="{{ route('import') }}" method="POST" enctype="multipart/form-data">
            @csrf
            <div class="row justify-content-center">
                <div class="col-md-6">
                    <div class="card">
                        <div class="card-header">
                            Import
                        </div>
                        <div class="card-body">
                            <div class="form-group">
                                <label for="type" class="required">
                                    File
                                </label>
                                <div class="controls">
                                    <input
                                        type="file"
                                        name="file"
                                        id="file"
                                        required
                                        accept=".csv,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel"
                                    />
                                </div>
                            </div>
                        </div>
                        <div class="card-footer">
                            <button class="btn btn-success w-100">Submit</button>
                        </div>
                    </div>
                </div>
            </div>
        </form>
DhPandya's avatar

@sebasti4n Please add your controller function here where you're validating the form or the request class.

jlrdw's avatar

@sebasti4n if it's ods, you could try

application/vnd.oasis.opendocument.spreadsheet

ezenteno's avatar

Try this 'file' => 'required|mimes:xlsx,xls,doc,docx,ppt,pptx,csv',

Please or to participate in this conversation.