Gulmuhammad Akbari
Gulmuhammad Akbari

Reputation: 2036

Laravel Upload File Mime type error

I got some errors with the following code, I searched a lot but I didn't get the solution. I want to upload file.xls, am I wrong? Thanks

My HTML Form:

 <form class="form-horizontal" role="form" method="post" action="{{ URL::to('doc/test') }}" enctype="multipart/form-data">
  <input style='width:400px;' type='file' id='files'  name='files[]' class="form-control" multiple='multiple'>
  <button type="submit" class="btn btn-primary">Save</button>
</form>

My Controller Function

public function uploadDocs()
        {
            $files = Input::file('files');
            $errors = "";
            foreach($files as $file) 
            {
              $rules = array('file' => 'mimes:png,gif,jpeg,pdf,doc,docx,xls,xlsx,max:1000000'); //'required|mimes:png,gif,jpeg,txt,pdf,doc'
              $validator = Validator::make(array('file'=> $file), $rules);
              if($validator->passes())
              {
                $destinationPath = 'documents';
                $filename = $file->getClientOriginalName();
                $temp = explode(".", $filename);
                $extension = end($temp);
                $filename = $temp[0].'_'.date('Y-m-d H:i:s').$extension;
                $upload_success = $file->move($destinationPath, $filename);
              } 
              else 
              {
                return Redirect::back()->withErrors($validator);
              }
            }

        }

Errors:

The file must be a file of type: png, gif, jpeg, pdf, doc, docx, xls, xlsx, max:1000000.

Upvotes: 2

Views: 10727

Answers (4)

Mixx
Mixx

Reputation: 11

Laravel 6:

I tried uploading video with the webm extension.

dd($request) return mimetype:video/webm, respectively, I tried to use it in rules. But i got mimetype incorrect error.

I checked another $request->file('video')->getMimeType() and got video/x-matroska. And i used rules mimetypes:video/x-matroska. It worked for me.

Upvotes: 1

Andrew
Andrew

Reputation: 199

you should add this attr to your form element:

enctype="multipart/form-data"

Upvotes: 2

Gulmuhammad Akbari
Gulmuhammad Akbari

Reputation: 2036

I tried and i found the solutions.

// validating each file.
          $rules = array('file' => 'required'); 
          $validator = Validator::make(
                [
                    'file' => $file,
                    'extension'  => \Str::lower($file->getClientOriginalExtension()),
                ],
                [
                    'file' => 'required|max:100000',
                    'extension'  => 'required|in:jpg,jpeg,bmp,png,doc,docx,zip,rar,pdf,rtf,xlsx,xls,txt'
                ]
            );

          if($validator->passes())
          {

            // path is root/uploads
            $destinationPath = 'documents';
            $filename = $file->getClientOriginalName();

            $temp = explode(".", $filename);
            $extension = end($temp);

            $filename = $temp[0].'_'.date('Y-m-d H:i:s').'.'.$extension;

            $upload_success = $file->move($destinationPath, $filename);

            if( $upload_success) 
            {
               return Response::json('success', 200);
            } 
            else 
            {
               $errors .= json('error', 400);
            }

          } 
          else 
          {
            // redirect back with errors.
            return Redirect::back()->withErrors($validator);
          }

Upvotes: -1

Rommy
Rommy

Reputation: 497

I also had problems with mime type validation through laravel's validator, so I ended up doing it manually with something like this

$supported_mime_types = array('application/vnd.ms-excel');
$file = Input::file('file');
$mime = $file->getMimeType();

if (!in_array($mime, $supported_mime_types)) {
  // mime type not validated
}

You can still keep the rule for the filesize

Upvotes: 3

Related Questions