Laravel 12 File Storage

Author

Kritim Yantra

Mar 24, 2025

Laravel 12 File Storage

Laravel 12, one of the most popular PHP frameworks, provides a powerful and easy-to-use file storage system. Whether you're building a simple blog or a complex web application, handling file uploads and storage is a common requirement. Laravel's file storage system simplifies this process by offering a clean, consistent API for working with local filesystems and cloud storage like Amazon S3.

In this blog, we'll explore Laravel's file storage system in detail, covering everything from configuration to practical examples. By the end of this guide, you'll have a solid understanding of how to manage file storage in Laravel 12.


Table of Contents

  1. Introduction to Laravel 12 File Storage
  2. Configuration
  3. Basic File Operations
    • Storing Files
    • Retrieving Files
    • Deleting Files
  4. Working with Public Files
  5. File Uploads
  6. Using Cloud Storage (Amazon S3)
  7. Conclusion

1. Introduction to Laravel File Storage

Laravel's file storage system is built on top of the Flysystem PHP package, which provides a unified API for interacting with various filesystems. Laravel 12 supports local storage, as well as cloud storage services like Amazon S3, FTP, and SFTP.

The file storage system is designed to be simple and intuitive, making it easy for developers to manage files in their applications.


2. Configuration

Before diving into file operations, let's configure the filesystem. Laravel's configuration file for file storage is located at config/filesystems.php. This file defines the available disks and their settings.

Here’s a snippet of the default configuration:

'disks' => [
    'local' => [
        'driver' => 'local',
        'root' => storage_path('app'),
    ],

    'public' => [
        'driver' => 'local',
        'root' => storage_path('app/public'),
        'url' => env('APP_URL').'/storage',
        'visibility' => 'public',
    ],

    's3' => [
        'driver' => 's3',
        'key' => env('AWS_ACCESS_KEY_ID'),
        'secret' => env('AWS_SECRET_ACCESS_KEY'),
        'region' => env('AWS_DEFAULT_REGION'),
        'bucket' => env('AWS_BUCKET'),
        'url' => env('AWS_URL'),
    ],
],
  • local disk: Stores files in the storage/app directory.
  • public disk: Stores files in storage/app/public, which is accessible via the web.
  • s3 disk: Stores files in an Amazon S3 bucket.

You can also create custom disks if needed.


3. Basic File Operations

Storing Files

To store a file, use the Storage facade. Here's an example of storing an uploaded file:

use Illuminate\Support\Facades\Storage;

public function storeFile(Request $request)
{
    if ($request->hasFile('file')) {
        $path = $request->file('file')->store('uploads', 'public');
        return "File stored at: " . $path;
    }

    return "No file uploaded.";
}
  • The store method saves the file to the specified directory (uploads) on the public disk.
  • The file is automatically given a unique name to avoid conflicts.

Retrieving Files

To retrieve a file, use the get method:

public function getFile($filename)
{
    $file = Storage::disk('public')->get('uploads/' . $filename);
    return response($file, 200)->header('Content-Type', 'image/jpeg'); // Adjust content type as needed
}
  • This example retrieves a file from the public disk and returns it as a response.

Deleting Files

To delete a file, use the delete method:

public function deleteFile($filename)
{
    Storage::disk('public')->delete('uploads/' . $filename);
    return "File deleted successfully.";
}

4. Working with Public Files

Files stored in the public disk can be accessed via the web. To make these files accessible, you need to create a symbolic link from public/storage to storage/app/public. Run the following Artisan command:

php artisan storage:link

Now, files stored in storage/app/public can be accessed via URLs like http://your-app.com/storage/uploads/filename.jpg.


5. File Uploads

Handling file uploads is a common use case. Here's a complete example:

Blade Form for File Upload

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

Controller Method for Handling Upload

public function upload(Request $request)
{
    if ($request->hasFile('file')) {
        $path = $request->file('file')->store('uploads', 'public');
        return "File uploaded successfully: " . $path;
    }

    return "No file uploaded.";
}

6. Using Cloud Storage (Amazon S3)

Laravel 12 makes it easy to use cloud storage services like Amazon S3. First, install the required package:

composer require league/flysystem-aws-s3-v3

Next, update your .env file with your S3 credentials:

AWS_ACCESS_KEY_ID=your-access-key
AWS_SECRET_ACCESS_KEY=your-secret-key
AWS_DEFAULT_REGION=your-region
AWS_BUCKET=your-bucket
AWS_URL=your-bucket-url

Now, you can use the s3 disk to store and retrieve files:

// Storing a file on S3
Storage::disk('s3')->put('uploads/file.jpg', file_get_contents($request->file('file')));

// Retrieving a file from S3
$file = Storage::disk('s3')->get('uploads/file.jpg');

7. Conclusion

Laravel's file storage system is a powerful tool for managing files in your application. Whether you're working with local storage or cloud services like Amazon S3, Laravel provides a clean and consistent API to simplify file operations.

In this guide, we covered:

  • Configuration of filesystems.
  • Basic file operations like storing, retrieving, and deleting files.
  • Handling file uploads.
  • Using cloud storage with Amazon S3.

With this knowledge, you're well-equipped to handle file storage in your Laravel 12 applications. Happy coding!

Tags

Laravel Php

Comments

No comments yet. Be the first to comment!

Please log in to post a comment:

Continue with Google

Related Posts