1

I am working on Laravel 8.x. My task is to validate .PDF file on server side. Everyting working fine but when I upload a valid PDF file it is not validating and returning with an error. My source code is following. Please correct my mistakes if you find

Thanks

blade file

<form class="w-100" id="addnotes" method="post" enctype="multipart/form-data" action="{{ route('upload-member-reports') }}">
    {{ csrf_field() }}
    <div class="modal-header">
    <h5 class="modal-title" name="report-file" id="exampleModalLongTitle">Upload Reports</h5>
    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
        <span aria-hidden="true">&times;</span>
    </button>
    </div>
    <div class="modal-body">
    <label class="file ">
        <input type="file" name="reportfile" id="reportfile"  aria-label="File browser example">
        <span class="file-custom"></span>
    </label>
    @if($errors->has('reportfile'))
        <div class="error text-danger">{{ $errors->first('reportfile') }}</div>
    @endif
    <input type="date" name="reportdate" class="form-control mt-20px" value="{{ old('reportdate') }}" placeholder="Select Date">
    <!-- <textarea type="textarea" rows="4" cols="50" class="form-control" placeholder="Enter Your Notes..."></textarea> -->
    @if($errors->has('reportdate'))
        <div class="error text-danger mt-5px">{{ $errors->first('reportdate') }}</div>
    @endif
    <input type="hidden" name="manager_id" value="{{ Auth::user()->id }}">
    <input type="hidden" name="member_id" value="{{ $id }}">
    </div>
    <div class="modal-footer">
        <input type="submit" class="btn btn-orange" value="Submit">
    </div>
</form>

controller file

public function uploadMemberReports( Request $request ){
    # Validation  Rules
    $rules = [
        'reportdate'=>'required',
        'reportfile' => 'required|mimes:pdf',
    ];
    $messages = [
        'reportdate.required' =>'Date is required.',
        'reportfile.required' => 'File is required.',
        'reportfile.mimes' => 'Only PDF files are allowed.',
    ];

    $validator = Validator::make( $request->all(), $rules, $messages);

    if ( $validator->fails() ) {
        # if validations fails redirect back with errors
        return redirect()->back()->withErrors($validator)->withInput();
    } else {
        # next action
    }
}

When I try with valid PDF file it returning with error like following

The reportfile failed to upload.

Edit: The request array response

Array
(
    [_token] => yaX0ohRjl6tR298Zd9WeSLcgxcoVQ9nPx3K5gO4S
    [reportdate] => 2021-01-12
    [manager_id] => 2
    [member_id] => 4
    [reportfile] => Illuminate\Http\UploadedFile Object
        (
            [test:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 
            [originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] => labreports_12.pdf
            [mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => application/octet-stream
            [error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 1
            [hashName:protected] => 
            [pathName:SplFileInfo:private] => 
            [fileName:SplFileInfo:private] => 
        )

)
Ram Chander
  • 1,088
  • 2
  • 18
  • 36

3 Answers3

2

This can have several causes. But Frontend looks good at first glance. It could be a server problem. For example FileSize, MimeType. You can output the request variable in the first step to rule out the possibility that the PDF has not arrived.

Edit (see at your comments): 'required|mimes:application/pdf'

Maik Lowrey
  • 15,957
  • 6
  • 40
  • 79
2

Simply add "enctype" into your form

<form method="POST"  enctype="multipart/form-data" action="your_action">

Basic Usage Of MIME Rule

'reportfile' => 'mimes:png'

Just try with full extensions

'reportfile' => 'mimes:application/pdf' 

OR

"reportfile" => 'mimetypes:application/pdf'

Full listing of MIME types and extensions https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types

suba
  • 1,320
  • 15
  • 22
0

Following above @maik Lowrey, I sucessfully used :

'file' => 'required|mimes:pdf|max:2048'

works very well ! ! ! (Answered here cause i don't have lvl to comment his answer)