File Upload and Storage

Laravel 114 views Dec 22, 2025 2 min read

Laravel provides a powerful and secure system for handling file uploads and managing file storage. Using the built-in Filesystem and Storage features, developers can easily upload, store, retrieve, and manage files locally or on cloud services.


1. File Upload Basics

File uploads are typically handled through HTML forms.

Form Example (Blade View)

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

  • @csrf protects against CSRF attacks


2. Handling File Uploads in Controller

use Illuminate\Http\Request; public function upload(Request $request) { $request->validate([ 'file' => 'required|file|mimes:jpg,png,pdf|max:2048', ]); $path = $request->file('file')->store('uploads'); return "File uploaded successfully: " . $path; }
  • store() automatically generates a unique filename

  • Files are stored in storage/app/uploads


3. File Validation Rules

Common file validation rules:

  • required

  • file

  • mimes:jpg,png,pdf

  • max:2048 (size in KB)

Example:

'image' => 'required|image|mimes:jpg,png|max:2048'

4. File Storage System

Laravel uses a disk-based storage system configured in:

config/filesystems.php

Common disks:

  • local

  • public

  • s3 (Amazon S3)


5. Public File Storage

To make files publicly accessible:

  1. Store file on the public disk:

$request->file('file')->store('uploads', 'public');
  1. Create a symbolic link:

php artisan storage:link

Public files will be accessible at:

public/storage/uploads

6. Custom File Names

$file = $request->file('file'); $filename = time().'_'.$file->getClientOriginalName(); $file->storeAs('uploads', $filename, 'public');

7. Retrieving Files

use Illuminate\Support\Facades\Storage; $url = Storage::url('uploads/file.jpg');

Displaying in Blade:


8. Downloading Files

return Storage::download('uploads/file.pdf');

9. Deleting Files

Storage::delete('uploads/file.jpg');

10. Cloud Storage (Amazon S3 Example)

Set credentials in .env:

FILESYSTEM_DISK=s3

Upload file:

$request->file('file')->store('uploads', 's3');

Conclusion

Laravel’s file upload and storage system makes handling files secure, flexible, and scalable. With built-in validation, multiple storage disks, and cloud support, Laravel allows developers to manage files efficiently while keeping applications clean and maintainable.

Some advanced sections are available for Registered Members
Share this Post
🚀 Want to Test Your Knowledge?

Take quizzes related to this topic and see where you stand!

Start Quiz Now
Back to Tutorials