Untitled

mail@pastecode.io avatar
unknown
php
a month ago
846 B
4
Indexable
Never
<?php

   namespace App\Http\Controllers;

   use Illuminate\Http\Request;
   use App\Models\File;

   class FileUploadController extends Controller
   {
       public function showUploadForm()
       {
           return view('upload');
       }

       public function uploadFile(Request $request)
       {
           $request->validate([
               'file' => 'required|file|mimes:jpg,png,pdf,docx|max:2048',
           ]);

           if ($request->file('file')) {
               $filePath = $request->file('file')->store('uploads', 'public');

               $file = new File();
               $file->file_path = $filePath;
               $file->save();

               return back()->with('success', 'File uploaded successfully.');
           }

           return back()->with('error', 'Please select a file to upload.');
       }
   }


Leave a Comment