0
0
LaravelHow-ToBeginner · 3 min read

How to Upload File in Laravel: Simple Guide with Example

To upload a file in Laravel, use the request()->file('input_name') method to get the file, then call store() or move() to save it. Validate the file first using $request->validate() to ensure it meets your requirements.
📐

Syntax

Uploading a file in Laravel involves these steps:

  • Get the file: Use $request->file('input_name') to access the uploaded file.
  • Validate: Use $request->validate() to check file type, size, etc.
  • Store the file: Use store() to save in storage or move() to save in a custom path.
php
<?php
public function upload(Request $request) {
    $request->validate([
        'file' => 'required|file|mimes:jpg,png,pdf|max:2048'
    ]);

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

    return $path;
}
💻

Example

This example shows a simple form and controller method to upload an image file and save it in the storage/app/uploads folder.

php
<?php
// routes/web.php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

Route::get('/upload', function () {
    return '<form method="POST" action="/upload" enctype="multipart/form-data">'
        . csrf_field()
        . '<input type="file" name="file" required>'
        . '<button type="submit">Upload</button>'
        . '</form>';
});

Route::post('/upload', function (Request $request) {
    $request->validate([
        'file' => 'required|file|mimes:jpg,png,pdf|max:2048'
    ]);

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

    return 'File uploaded to: ' . $path;
});
Output
File uploaded to: uploads/filename.jpg
⚠️

Common Pitfalls

Common mistakes when uploading files in Laravel include:

  • Not setting enctype="multipart/form-data" in the HTML form, so files don't get sent.
  • Skipping validation, which can cause security risks or errors.
  • Using move() without creating the destination folder first.
  • Not configuring filesystem permissions, causing storage failures.
php
<?php
// Wrong: Missing enctype in form
// <form method="POST" action="/upload">
// Correct:
// <form method="POST" action="/upload" enctype="multipart/form-data">

// Wrong: No validation
// $file = $request->file('file');
// $file->store('uploads');

// Correct:
// $request->validate(['file' => 'required|file|mimes:jpg,png|max:2048']);
// $file = $request->file('file');
// $file->store('uploads');
📊

Quick Reference

Summary tips for file upload in Laravel:

  • Always use enctype="multipart/form-data" in your form tag.
  • Validate files with $request->validate() before saving.
  • Use store() to save files in Laravel's storage folder.
  • Access uploaded files with $request->file('input_name').
  • Check and set correct permissions on storage folders.

Key Takeaways

Use $request->file('input_name') to get the uploaded file in Laravel.
Always validate uploaded files with $request->validate() to ensure safety and correctness.
Set enctype="multipart/form-data" in your HTML form to enable file uploads.
Use store() method to save files in Laravel's storage directory easily.
Check folder permissions to avoid storage errors when saving files.