0
0
Laravelframework~5 mins

Uploading files in Laravel

Choose your learning style9 modes available
Introduction

Uploading files lets users send pictures, documents, or other data to your website. It helps you save and use these files later.

When users need to add profile pictures to their accounts.
When customers upload documents for verification.
When users submit images or files in a form.
When you want to store user-generated content like photos or PDFs.
Syntax
Laravel
<?php

// In your controller method
public function upload(Request $request)
{
    $request->validate([
        'file' => 'required|file|max:10240', // max 10MB
    ]);

    $path = $request->file('file')->store('uploads');

    return back()->with('success', 'File uploaded to ' . $path);
}

Use store() to save files in the default storage folder.

Validate files to check size and type before saving.

Examples
This saves the file with its original name inside the 'uploads' folder.
Laravel
<?php
// Store file in 'uploads' folder with original name
$path = $request->file('file')->storeAs('uploads', $request->file('file')->getClientOriginalName());
This stores the file in the 'uploads' folder and makes it publicly accessible.
Laravel
<?php
// Store file publicly accessible
$path = $request->file('file')->store('uploads', 'public');
This ensures only JPG, PNG, or PDF files up to 5MB are accepted.
Laravel
<?php
// Validate file type and size
$request->validate([
    'file' => 'required|mimes:jpg,png,pdf|max:5120',
]);
Sample Program

This example shows a simple Laravel controller with two methods: one to display the upload form and one to handle the file upload. The form uses enctype="multipart/form-data" to send files. The controller validates the file and stores it in the 'uploads' folder. After uploading, it shows a success message with the file path.

Laravel
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class FileUploadController extends Controller
{
    public function showForm()
    {
        return view('upload-form');
    }

    public function upload(Request $request)
    {
        $request->validate([
            'file' => 'required|file|max:2048', // max 2MB
        ]);

        $path = $request->file('file')->store('uploads');

        return back()->with('success', "File uploaded to: $path");
    }
}

// Blade view: resources/views/upload-form.blade.php
//
// <form method="POST" action="{{ route('file.upload') }}" enctype="multipart/form-data">
//     @csrf
//     <input type="file" name="file" aria-label="Choose file to upload" required>
//     <button type="submit">Upload</button>
// </form>
//
// @if(session('success'))
//     <p>{{ session('success') }}</p>
// @endif
OutputSuccess
Important Notes

Always use enctype="multipart/form-data" in your HTML form to allow file uploads.

Use Laravel's validation to protect your app from unwanted file types or large files.

Files are stored in the storage/app folder by default. Use php artisan storage:link to make files accessible publicly.

Summary

Uploading files lets users send data to your app.

Use Laravel's store() method to save files easily.

Always validate files and set proper form encoding.