Complete guide to uploading files in Laravel 11

Diego Cortés
Diego Cortés
January 21, 2025
Complete guide to uploading files in Laravel 11

Currently, file management is a fundamental part of many web applications. Laravel, as one of the most popular development frameworks, offers robust and efficient tools for file uploads. In this article, I present a complete guide on how to accomplish this task in Laravel 11, covering everything from initial setup to implementation in a project.

Table of Contents

Prerequisites

Before you begin, you need to have Laravel 11 installed and have a local server or domain to test your application. Additionally, it is recommended to have basic knowledge of PHP and a general understanding of how Laravel works.

Environment Setup

To start uploading files, you first need to ensure your environment is properly configured. Open the terminal and navigate to your Laravel project folder. Also, make sure that the directory where you will store the files has write permissions.

Route Configuration

In the routes/web.php file, you need to define a route that will handle the request to upload files. You can add something like the following:

Route::get('upload', 'FileUploadController@create');
Route::post('upload', 'FileUploadController@store');

This allows your application to respond to a GET request to display the file upload form and a POST request to process the upload.

Creating the Controller

Next, create a controller that will manage the file upload logic. Use the following command in your terminal:

php artisan make:controller FileUploadController

Within the controller, define two methods: create to show the form and store to handle the file upload. Here’s a simple example:

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class FileUploadController extends Controller
{
    public function create()
    {
        return view('upload');
    }

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

        $file = $request->file('file');
        $fileName = time() . '_' . $file->getClientOriginalName();
        $file->move(public_path('uploads'), $fileName);

        return back()->with('success', 'File successfully uploaded: ' . $fileName);
    }
}

Creating the View

Now it’s time to create the view where the user can select a file to upload. Create a view file at resources/views/upload.blade.php with the following content:

<!DOCTYPE html>
<html>
<head>
    <title>Upload File</title>
</head>
<body>
    <h1>Upload File</h1>
    @if (session('success'))
        <p>{{ session('success') }}</p>
    @endif
    <form action="{{ url('upload') }}" method="POST" enctype="multipart/form-data">
        @csrf
        <input type="file" name="file" required>
        <button type="submit">Upload</button>
    </form>
</body>
</html>

Here, the form includes an input for file selection and a button to submit it. The @csrf directive is used to protect against CSRF attacks, ensuring that only legitimate requests from your application are accepted.

Testing File Upload

Now that you have set up the controller and the view, it’s time to test the functionality. Open your browser and navigate to the route you defined for the upload form. When you select a file and submit it, you should see a success message.

Conclusion

Uploading files in Laravel 11 is a straightforward process that, with the right configuration, you can quickly implement in your application. This functionality not only enhances user interaction but is also essential for many types of web applications.

I invite you to keep reading more news and tutorials about web development on my blog, where I continue to share useful guides and practical tips. Until next time!

Article information

Published: January 21, 2025
Category: Laravel Tutorials
Reading time: 5-8 minutes
Difficulty: Intermediate

Key tips

1

Take your time to understand each concept before moving on to the next one.

2

Practice the examples in your own development environment for better understanding.

3

Don't hesitate to review the additional resources mentioned in the article.

Diego Cortés
Diego Cortés
Full Stack Developer, SEO Specialist with Expertise in Laravel & Vue.js and 3D Generalist

Frequently Asked Questions

Categories

Page loaded in 23.37 ms