pathsush's avatar

How to upload the pdf/doc file in laravel through validation

I am trying to make a website where the user will be able to upload the pdf and doc file. Also, I want to save the file in the storage folder so I made the symlink so it would replicate the folder in the Public folder. SO I have a folder called Public/storage/Paper in public. This is my form where the user will be able to upload the paper.

I am trying to make a website where the user will be able to upload the pdf and doc file. Also, I want to save the file in the storage folder so I made the symlink so it would replicate the folder in the Public folder. SO I have a folder called Public/storage/Paper in public. This is my form where the user will be able to upload the 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>
On my controller, I wrote this code, this works perfectly fine like saving the data in the database when I don't need to upload the file. But when I need to upload the paper the validation doesn't pass.
<?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(),['paper'=>"required|string|mimes:pdf,zip"])->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, "paper"=>$fileName);
             $created= DB::table('submissions')->insert($newsubmission);
             if($created){
                  return "Sucessful";
                         }else{
                    return "Not Sucessful";
                                }
                          }
                        }

I am not being able to detect the problem. Any kind of help will be appreciated.

0 likes
1 reply
mvd's avatar

Hi @pathsush

And what if you remove this line (you dont check if the validation passed/fails here)

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

And change

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

To

$request->validate([
        'title'=>'required',
         'paper'=>'nullable|mimes:pdf,zip'
 ]);

And remove the 'require' from your form field.

<input type="file" class="form-control" name="paper" >

Now the paper file is optional (paper file is not required), if there is a file there is validation to check it's a pdf or zip.

Not sure if the paper file always must be required. But if this is the case change nullable to required off course

Please or to participate in this conversation.