agyanadda@gmail.com

Laravel File Upload

Forms, Security and APIs 1 min read Updated Dec 2, 2026 Topic 22 of 31

Laravel's Storage system makes file uploads short and safe.

1. The form

The form must use method="POST" and enctype="multipart/form-data", otherwise the file is not sent.

2. Validate

'photo' => 'required|image|mimes:jpg,jpeg,png,webp|max:2048' — max is in kilobytes (2048 = 2 MB).

3. Store

$path = $request->file('photo')->store('photos', 'public');

Laravel gives the file a random unique name and returns a path like photos/aB3x9.jpg. Save that path in the database.

4. Make it public

Run php artisan storage:link once. It links public/storage to storage/app/public, so files on the public disk get a URL.

5. Show and delete

  • Show: <img src="{{ asset('storage/' . $user->photo) }}">
  • Delete: Storage::disk('public')->delete($user->photo)

Private files

Files such as invoices or ID documents should go on the default local disk (not public) and be sent through a controller that checks permission, using Storage::download($path).

Example

Example — Upload a profile photo PHP
<?php
use Illuminate\Support\Facades\Storage;

public function updatePhoto(Request $request)
{
    $request->validate([
        'photo' => 'required|image|mimes:jpg,jpeg,png,webp|max:2048',
    ]);

    $user = $request->user();

    if ($user->photo) {
        Storage::disk('public')->delete($user->photo);   // remove the old photo
    }

    $user->update([
        'photo' => $request->file('photo')->store('photos', 'public'),
    ]);

    return back()->with('success', 'Photo updated');
}

/* Blade form:
<form method="POST" action="{{ route('profile.photo') }}" enctype="multipart/form-data">
    @csrf
    <input type="file" name="photo" accept="image/*">
    <button>Upload</button>
</form> */

The file is saved as storage/app/public/photos/<random-name>.jpg and shown with asset('storage/' . $user->photo). The old photo is deleted first.

Test Yourself With Exercises

Exercise 1 Multiple Choice

Which form attribute is required for file uploads?

Exercise 2 True / False

In the rule max:2048 for a file, 2048 means kilobytes.

Exercise 3 Fill in the Blank

Run php artisan storage:___ to make the public disk reachable from the web.

Frequently Asked Questions

Usually php artisan storage:link has not been run on that server, or the path is wrong. Check that public/storage exists and that you use asset('storage/' . $path). On some shared hosting, symlinks are blocked and you need a different setup.