2

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.

4 Answers 4

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

I found another solution for this problem and i will post it.
which is basically the same, lol, and doesn't make the "official" mime validation working
2

you should add this attr to your form element:

enctype="multipart/form-data"

Comments

1

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.

Comments

-1

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);
          }

1 Comment

This just allows all files. Anyway getClientOriginalExtension is insecure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.