Home > Web Development > Laravel Tutorials > How to easily upload images with Laravel in your project

How to easily upload images with Laravel in your project

Diego Cortés
Diego Cortés
January 20, 2025
How to easily upload images with Laravel in your project

Laravel has established itself as a very popular PHP framework for web application development. One of the most common tasks that a developer must face is uploading images in their project. Below is a detailed explanation of how this process can be carried out easily using Laravel.

Initial Preparations

Before starting with image uploads, it's important to ensure that your development environment is properly configured. You should have Laravel installed and a project created. Additionally, make sure that the directory where you will save the images has the necessary permissions for your application to write to it.

Create the Upload Form

The first step to uploading images is creating a form where users can select the file they wish to upload. This form can be implemented in a Blade view in Laravel. Here is a basic example:

<form action="{{ route('image.upload') }}" method="POST" enctype="multipart/form-data">
    @csrf
    <input type="file" name="image" required>
    <button type="submit">Upload Image</button>
</form>

This form includes a file type input and a submit button. The @csrf directive is important for protecting the application against CSRF attacks.

Define the Route

Now, you need to define a route in your Laravel route files that points to the controller responsible for handling the image upload. Add the following in your web.php file:

Route::post('/image/upload', [ImageController::class, 'upload'])->name('image.upload');

In this case, ImageController will be the controller that manages the upload logic.

Create the Controller

Next, create a controller that contains the logic for handling the image uploads. You can generate it using the Artisan command:

php artisan make:controller ImageController

Then, add the upload method in the controller:

use Illuminate\Http\Request;

class ImageController extends Controller
{
    public function upload(Request $request)
    {
        $request->validate([
            'image' => 'required|image|mimes:jpeg,png,jpg,gif|max:2048',
        ]);

        if ($request->file('image')->isValid()) {
            $imageName = time() . '.' . $request->image->extension();  
            $request->image->move(public_path('images'), $imageName);

            return back()->with('success', 'Image uploaded successfully.')->with('image', $imageName);
        }

        return back()->withErrors('Error uploading image.');
    }
}

In this code, we use validate() to ensure that the file the user is trying to upload is an image and meets certain requirements, such as maximum size and allowed formats. If the validation is successful, we move the image to the public images folder.

Managing Responses

After the image has been successfully uploaded, you can send a response to the user confirming that the image was uploaded. In the previous example, with() is used to send a success message.

Conclusion

Uploading images in a Laravel project is a straightforward process if you follow the right steps. From creating the form to implementing the controller, Laravel makes file handling easier in your applications.

To discover more interesting and useful topics about web development and Laravel, I invite you to continue exploring the news on my blog. Your next lesson awaits you!

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

Categories

Page loaded in 22.29 ms