trafficker
trafficker

Reputation: 601

Validate files in Laravel 5.8

I have validation:

if ($request->hasFile('otherFiles')) {
            $this->validate($request, [
                'otherFiles' => 'mimes:image/jpeg'
            ]);
        }

How can I add to this validation files type: DOC, DOCX, CSV, PDF, RTF, PNG, xlsx, XLS, TXT, BMP?

Upvotes: 0

Views: 2336

Answers (2)

Ahmed Aboud
Ahmed Aboud

Reputation: 1322

'otherFiles.*' => 'mimes:jpeg,bmp,png'

for full mime type

'otherFiles.*' => 'mimetypes:image/jpeg,image/bmp,image/png'

mime type validation

for list of all mime types and their extensions mime types

Upvotes: 4

trafficker
trafficker

Reputation: 601

i have this code:

if ($request->hasfile('otherFiles')) {
            $this->validate($request, [
                'otherFiles' => 'required',
                'otherFiles.*' => 'mimes:jpg,jpeg,bmp,png,doc,docx,csv,rtf,xlsx,xls,txt,pdf'
            ]);
            foreach ($request->file('otherFiles') as $file) {
                $extension = strtolower($file->getClientOriginalExtension());
                $path = 'upload/other/';
                $uniqueName = md5($file . time());
                $file->move(public_path($path), $uniqueName . '.' . $extension);
            }
        } 

and I have error: "otherFiles":["other files must file type image/jpeg."

Upvotes: 2

Related Questions