Home > Software engineering >  Can't store Files in public folder in Laravel 8
Can't store Files in public folder in Laravel 8

Time:04-30

i'm trying to store pdf, txt... in DB but i get this error:

Method Illuminate\Validation\Validator::validateCsv,txt,xlx,xls,pdf does not exist.

but when i add required|mimes this error i get:

The file must be a file of type: pdf, xlx, csv, txt, doc.

here the code:

    public function store(Request $request)
    {
        $validatedData = $request->validate([
            'file' => 'required|mimes:pdf,xlx,csv,txt,doc'
        ]);
        $name = $request->file('file')->getClientOriginalName();
        $path = $request->file('file')->store('public/files');
        $save = new File;
        $save->name = $name;
        $save->path = $path;
        return redirect('file-upload')->with('status', 'File Has been uploaded successfully');

    }
    

my blade code:

<body>
<div >
      <form method="post" enctype="multipart/form-data" id="upload-file" action="{{ url('store') }}" >
          <div >
              <div >
                  <div >
                      <input type="file" name="file" placeholder="Choose file" id="file">
                        @error('file')
                        <div >{{ $message }}</div>
                        @enderror
                  </div>
              </div>
                
              <div >
                  <button type="submit"  id="submit">Submit</button>
              </div>
          </div>     
      </form>
</div>
</div>  
</body>

how can i fix it?

CodePudding user response:

try to dump the file on your controller before the validator and see if you really get the correct file type that you specified.

dd($request->file);

CodePudding user response:

Use File to put content

\File::put(public_path('storage'). '/public/files/'. $file);
  • Related