Laravel makes it very easy to store uploaded files using the store method on an uploaded file instance. Laravel call the store method with the path at which you wish to store the uploaded file.

Create Routes

routes/web.php : In next step, we will add new two routes in web.php file.

Route::get('/', function () {
    return view('fileuploading');
});
Route::post("fileuploading","UserController@fileuploading");

Creating The FileUploadController

Create a controller file UserController.php and save it in the following path app/Http/Controllers/UserController.php.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class UserController extends Controller {
  
   public function fileuploading(Request $request) {
        $fileName = time().$request->file->getClientOriginalName();  
   
        $request->file->move(public_path('uploads'), $fileName);
   
        return back()
            ->with('success','successfully upload file.');
   }
   
}
?>

Creating The View

Create a view file fileuploading.blade.php and save the below code it in resources/views/fileuploading.blade.php.

<!DOCTYPE html>
<html>
    <head>
        <title>Login form</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
    </head>
    <body>
        <div class="container" style="margin-top: 50px">
            <div class="row">
                <form action="fileuploading" method="post" enctype="multipart/form-data">
                    @csrf
                    <div class="row">
                        
                        <div class="col-md-6">
                            @if ($message = Session::get('success'))
                            <div class="alert alert-success alert-block">
                                <button type="button" class="close" data-dismiss="alert">×</button>
                                    <strong>{{ $message }}</strong>
                            </div>
                            @endif
                            <input type="file" name="file" class="form-control">
                            <br />
                            <button type="submit" class="btn btn-primary">Upload</button>
                        </div>

                    </div>
                </form>
            </div>
        </div>
    </body>
</html>

Let us execute this example by the following URL in the browser. This URL may be different based on your website.

https://howtowebcode.com/