0
0
Laravelframework~5 mins

File uploads in Laravel

Choose your learning style9 modes available
Introduction

File uploads let users send files like images or documents to your website. Laravel makes handling these uploads easy and safe.

Allow users to upload profile pictures on their account page.
Let customers attach files when submitting a support ticket.
Enable uploading product images in an online store admin panel.
Accept resumes or documents in a job application form.
Syntax
Laravel
<?php

// In your controller method handling the form submission
public function uploadFile(Request $request) {
    $request->validate([
        'file' => 'required|file|mimes:jpg,png,pdf|max:2048',
    ]);

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

    return back()->with('success', 'File uploaded to ' . $path);
}

Use $request->file('input_name') to get the uploaded file.

The store method saves the file in the storage/app/uploads folder by default.

Examples
This example only accepts image files up to 1MB and stores them in the 'images' folder.
Laravel
<?php
// Validate and store an image file
$request->validate(['image' => 'required|image|max:1024']);
$path = $request->file('image')->store('images');
This saves the uploaded file in 'docs' folder using its original filename.
Laravel
<?php
// Store file with original name
$file = $request->file('document');
$filename = $file->getClientOriginalName();
$file->storeAs('docs', $filename);
Always check if a file was uploaded before trying to store it.
Laravel
<?php
// Check if file was uploaded
if ($request->hasFile('photo')) {
    $path = $request->file('photo')->store('photos');
}
Sample Program

This example shows a simple Laravel controller with two methods: one to show the upload form and one to handle the file upload. The form uses enctype="multipart/form-data" to allow file sending. The controller validates the file type and size, stores it, and returns a success message.

Laravel
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class FileUploadController extends Controller
{
    public function showForm()
    {
        return view('upload-form');
    }

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

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

        return back()->with('success', 'File uploaded to ' . $path);
    }
}

// Blade view: resources/views/upload-form.blade.php
//
// <form method="POST" action="{{ route('file.upload') }}" enctype="multipart/form-data">
//     @csrf
//     <input type="file" name="file" aria-label="Choose file to upload" required>
//     <button type="submit">Upload</button>
// </form>
//
// @if(session('success'))
//     <p>{{ session('success') }}</p>
// @endif
OutputSuccess
Important Notes

Always validate uploaded files to avoid security risks.

Use enctype="multipart/form-data" in your HTML form to enable file uploads.

Files are stored in storage/app by default. Use php artisan storage:link to make them accessible publicly.

Summary

Laravel makes file uploads simple with built-in methods.

Always validate files for type and size before saving.

Use proper form encoding and check if files exist before processing.