【问题标题】:How to upload the pdf/doc file in laravel through validation如何通过验证在laravel中上传pdf/doc文件
【发布时间】:2020-03-03 00:13:23
【问题描述】:

我正在尝试创建一个用户可以上传 pdf 和 doc 文件的网站。另外,我想将文件保存在存储文件夹中,所以我创建了符号链接,以便复制公共文件夹中的文件夹。所以我有一个名为 Public/storage/Paper 的公共文件夹。这是我的表单,用户可以在其中上传论文。

<form class="form-horizontal" method="post" action="paper" enctype="multipart/form-data">
    @csrf
    <div class="form-group">
        <label for="title">Title of Paper</label>
        <input type="text" class="form-control" name="title"  placeholder="Title of Paper" required="required">
    </div>
    <div class="form-group">
        <label for="Paper">Upload Paper</label>
        <input type="file" class="form-control" name="paper" required="required" >
    </div>

    <button type="submit" class="btn btn-primary">Submit</button>
</form>

在我的控制器上,我编写了这段代码,当我不需要上传文件时,这就像将数据保存在数据库中一样完美。但是当我需要上传论文时,验证没有通过。

 <?php

 namespace App\Http\Controllers;
 use App\Submission;
 use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Facades\Validator;
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\File;
 use Illuminate\Support\Facades\Storage;
 class PapersController extends Controller
{


public function  store(Request $request){

    $request->validate([
        'title'=>'required',
        'paper'=>'required'

    ]);


    $title= $request->input('title');
    Validator::make($request->all(),['file'=>"required|string|paper|mimes:pdf,docx"])->validate();
    $extension= $request->file("file")->getClientOriginalExtension();
    $stringPaperFormat=str_replace(" ", "", $request->input('title'));

    $fileName= $stringPaperFormat.".".$extension;
   $FileEnconded=  File::get($request->paper);
    Storage::disk('local')->put('public/Paper'.$fileName, $FileEnconded);
    $newsubmission= array("title"=>$title, "first_name"=>$first_name, "last_name"=>$last_name,"isReviewed"=>$isReviewed, "paper"=>$fileName);
    $created= DB::table('submissions')->insert($newsubmission);
    if($created){
        return "Sucessful";
    }else{
        return "Not Sucessful";
    }


}

}

我无法检测到问题。任何形式的帮助将不胜感激。

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    实际上docdocszip 文件,所以您需要在验证规则中添加zip 而不是docdocx

    您的字段名称也是paper,并且您在创建验证时使用file。将file 更改为paper。而且您在文件上传验证中使用了paper 规则,该规则不是内置验证规则,因此也将其删除。

    改变

     Validator::make($request->all(),['file'=>"required|string|paper|mimes:pdf,docx"])->validate();
    

     Validator::make($request->all(),['paper'=>"required|string|mimes:pdf,zip"])->validate();
    

    参考资料: Laracast -> How to make validation rules to upload pdf, doc and docx files in laravel 5.4

    https://en.wikipedia.org/wiki/Office_Open_XML

    【讨论】:

    • 谢谢@Sehdev,我确实进行了更改,但仍然无法正常工作。你能帮帮我吗?
    • 尝试所有这些 mime:doc、pdf、docx、zip。此外,您的字段名称似乎是纸质的,并且您在创建验证时正在使用 file
    • 另外,您的字段名称似乎是 paper 并且您在创建验证时正在使用 file。将file 更改为paper
    • 是的,当我像这样将文件更改为纸质文件时,Validator::make($request->all(),['paper'=>"required|string|paper|mimes:pdf, zip"])->验证();它抛出一个错误方法 Illuminate\Validation\Validator::validatePaper 不存在。
    • 您在验证规则required|string|paper 中定义了paper 规则。从中删除paper。试试这个 Validator::make($request->all(),['paper'=>"required|string|mimes:pdf,zip"])->validate();
    猜你喜欢
    • 2020-06-22
    • 1970-01-01
    • 1970-01-01
    • 2018-05-07
    • 1970-01-01
    • 2023-03-09
    • 2017-02-08
    • 2019-04-30
    • 2021-08-03
    相关资源
    最近更新 更多