Uploading Files with Laravel Forms

Tutorial 4 of 5

Introduction

In this tutorial, our main goal is to learn how to upload files using Laravel forms. We will be looking into handling file inputs, validating them, and finally storing them in our Laravel application. By the end of this tutorial, you will be able to create a form in Laravel that can upload files, validate file inputs, and store the file in your server.

Before we start, ensure that you have a basic understanding of PHP, Laravel, and HTML forms. Knowledge of MVC (Model-View-Controller) architecture will also be beneficial.

Step-by-Step Guide

File Inputs

In Laravel, you can handle file inputs using the file method on the Request instance. This method will return an instance of Illuminate\Http\UploadedFile, which extends the PHP SplFileInfo class and provides various methods for interacting with the file.

Let's say we have a form that contains a file input:

<form action="/upload" method="POST" enctype="multipart/form-data">
    @csrf
    <input type="file" name="photo">
    <input type="submit" value="Upload">
</form>

To retrieve the file from the request, we can use the file method:

$photo = $request->file('photo');

Validation

Before storing the file, you may want to validate it. Laravel provides several validation rules for files, including file, image, mimes, and size.

Here's how you can validate that the uploaded file is an image and does not exceed 2048 kilobytes:

$request->validate([
    'photo' => 'required|image|max:2048',
]);

Storing Files

After validating the file, you can store it using the store method:

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

By default, this method will store the file in the storage/app directory. If you want to specify a different disk, you can use the storeOn method:

$path = $request->file('photo')->storeOn('s3', 'photos');

Code Examples

Let's put everything together in a controller method:

use Illuminate\Http\Request;

class PhotoController extends Controller
{
    public function upload(Request $request)
    {
        // Validate the request
        $request->validate([
            'photo' => 'required|image|max:2048',
        ]);

        // Store the photo
        $path = $request->file('photo')->store('photos');

        // Return the path
        return $path;
    }
}

In this method, we first validate the request. If the validation fails, Laravel will automatically redirect the user back to the form and flash the error messages to the session.

Next, we store the photo in the storage/app/photos directory. The store method returns the path of the stored file, which we then return.

Summary

In this tutorial, we learned how to upload files using Laravel forms. We started by handling file inputs using the file method on the Request instance. Then, we validated the file input using the validate method. Finally, we stored the file using the store method.

For further learning, you can explore other ways to store files, such as storing them on an external disk like Amazon S3.

Practice Exercises

  1. Create a form that allows users to upload multiple photos at once. Modify the upload method to handle multiple files.

  2. Add more validation rules to the upload method. For example, you can restrict the file types to jpeg and png, and set a minimum file size.

  3. Instead of storing the file in the storage/app/photos directory, store it in a public directory that can be accessed directly from the web.

Remember to test your code thoroughly after each change. Happy coding!