DEV Community

Dhiecoderweb
Dhiecoderweb

Posted on Originally published at dhiecoderweb.com

How to Upload Multiple Files in Laravel

File upload is one of the common needs in web applications. Laravel provides an easy and secure way to handle file uploads, including uploading multiple files at once. In this tutorial, you'll learn how to build a multiple file upload feature in Laravel from scratch, complete with validation and storage.

Storage Preparation

Laravel uses a filesystem to manage files. First, make sure you've created the symbolic link so uploaded files can be accessed publicly:

php artisan storage:link
Enter fullscreen mode Exit fullscreen mode

This command creates a link from public/storage to storage/app/public. Files stored in storage/app/public/ will be accessible via URL.

Creating the Migration and Model

Create a table to store the uploaded file data:

php artisan make:model UploadedFile -m
Enter fullscreen mode Exit fullscreen mode

Edit the migration file:

public function up(): void
{
    Schema::create('uploaded_files', function (Blueprint $table) {
        $table->id();
        $table->string('original_name');
        $table->string('stored_name');
        $table->string('path');
        $table->string('mime_type');
        $table->unsignedBigInteger('size');
        $table->timestamps();
    });
}
Enter fullscreen mode Exit fullscreen mode

Edit the model app/Models/UploadedFile.php:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class UploadedFile extends Model
{
    protected $fillable = [
        'original_name',
        'stored_name',
        'path',
        'mime_type',
        'size',
    ];
}
Enter fullscreen mode Exit fullscreen mode

Run the migration:

php artisan migrate
Enter fullscreen mode Exit fullscreen mode

This is only part of the article. For the full discussion, with examples and step-by-step details, you can read it on the original source:

How to Upload Multiple Files in Laravel

More programming tutorials and developer tools are also available at dhiecoderweb.com.

Top comments (0)