【问题标题】:Laravel 8.x PDF file validation is not working correctlyLaravel 8.x PDF 文件验证无法正常工作
【发布时间】:2021-04-17 06:18:58
【问题描述】:

我正在开发 Laravel 8.x。我的任务是在服务器端验证 .PDF 文件。一切正常,但是当我上传有效的 PDF 文件时,它没有验证并返回错误。 我的源代码如下。如果你发现了,请纠正我的错误

谢谢

刀片文件

<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>

控制器文件

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

当我尝试使用有效的 PDF 文件时,它返回如下错误

报告文件上传失败。

编辑: 请求数组响应

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] => 
        )

)

【问题讨论】:

  • 试试'reportfile' =&gt; 'required|mimes:application/pdf',
  • @sta 已经尝试过了,但是没有用。

标签: laravel laravel-8 laravel-validation laravel-filesystem


【解决方案1】:

这可能有多种原因。但是 Frontend 乍一看还不错。可能是服务器问题。例如 FileSize、MimeType。可以在第一步输出请求变量,排除PDF没有到达的可能。

编辑(请参阅您的 cmets):'required|mimes:application/pdf'

【讨论】:

  • 已经尝试过 - 'required|mimes:application/pdf'。仍然遇到同样的问题
  • 查看$request的数组响应。我更新了问题
  • 谢谢!我看到的是 [hashName:protected] 和 [pathName:SplFileInfo:private] 没有值。也许增加你在服务器上的文件大小。或尝试使用一个小的 pdf 文件。
  • 它适用于小尺寸 PDF 文件。非常感谢您的帮助:)
【解决方案2】:

只需在表单中添加“enctype”

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

MIME 规则的基本用法

'reportfile' => 'mimes:png'

只需尝试完整的扩展程序

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

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

MIME 类型和扩展名的完整列表https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types

【讨论】:

  • 已经尝试过 - 'required|mimes:application/pdf'。仍然遇到同样的问题
  • @RamChander 我在带有 laragon 的 Windows 中使用 laravel 8.x 时遇到了同样的问题。你解决了这个问题吗?
  • 我解决了添加 enctype="multipart/form-data" 到表单
  • @FreddyDaniel 是的,我已经解决了这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-17
  • 2016-11-09
  • 2020-04-24
  • 2017-11-25
  • 1970-01-01
  • 2016-04-02
  • 1970-01-01
相关资源
最近更新 更多