Laravel 10 File Upload Example Tutorial

Laravel 10,Laravel provides built-in support for handling file uploads, making it simple to implement file upload functionality in your application.

Step 1: Install Laravel App

composer create-project laravel/laravel example-FileUpload

Step 2: Create Controller

php artisan make:controller FileController


    namespace App\Http\Controllers;
    use Illuminate\Http\Request;
    use Illuminate\View\View;
    use Illuminate\Http\RedirectResponse;
    class FileController extends Controller
         * Display a listing of the resource.
         * @return \Illuminate\Http\Response
        public function index(): View
            return view('fileUpload');
         * Display a listing of the resource.
         * @return \Illuminate\Http\Response
        public function store(Request $request): RedirectResponse
                'file' => 'required|mimes:pdf,xlx,csv|max:2048',
            $fileName = time().'.'.$request->file->extension();  
            $request->file->move(public_path('uploads'), $fileName);
            if ($request->file('file')){
            $fileName = time().rand(1,99).'.'.$file->extension();  
            $file->move(public_path('uploads'), $fileName);
            $file= $fileName;
            return back()
                ->with('success','You have successfully upload file.')
                ->with('file', $fileName);

Store File in Storage Folder

$request->file->storeAs('uploads', $fileName);
// storage/app/uploads/file.png

Store File in Public Folder

$request->file->move(public_path('uploads'), $fileName);
    // public/uploads/file.png

Store File in S3

$request->file->storeAs('uploads', $fileName, 's3');

Step 3: Create and Add Routes


    use Illuminate\Support\Facades\Route;
    use App\Http\Controllers\FileController;
    | Web Routes
    | Here is where you can register web routes for your application. These
    | routes are loaded by the RouteServiceProvider within a group which
    | contains the "web" middleware group. Now create something great!
    Route::get('file-upload', [FileController::class, 'index']);
    Route::post('file-upload', [FileController::class, 'store'])->name('');

Step 4: Create Blade File


<!DOCTYPE html>
        <title>Laravel 10 File Upload Example -</title>
        <link href="" rel="stylesheet">
    <div class="container">
        <div class="panel panel-primary">
          <div class="panel-heading">
            <h2>Laravel 10 File Upload Example -</h2>
          <div class="panel-body">
            @if ($message = Session::get('success'))
                <div class="alert alert-success alert-block">
                    <strong>{{ $message }}</strong>
            <form action="{{ route('') }}" method="POST" enctype="multipart/form-data">
                <div class="mb-3">
                    <label class="form-label" for="inputFile">File:</label>
                        class="form-control @error('file') is-invalid @enderror">
                        <span class="text-danger">{{ $message }}</span>
                <div class="mb-3">
                    <button type="submit" class="btn btn-success">Upload</button>

Run Laravel App:

php artisan serve

Now, Go to web browser, type the given URL and see the output: