Kritim Yantra
Mar 24, 2025
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.
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.
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.
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.";
}
store
method saves the file to the specified directory (uploads
) on the public
disk.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
}
public
disk and returns it as a response.To delete a file, use the delete
method:
public function deleteFile($filename)
{
Storage::disk('public')->delete('uploads/' . $filename);
return "File deleted successfully.";
}
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
.
Handling file uploads is a common use case. Here's a complete example:
<form action="/upload" method="POST" enctype="multipart/form-data">
@csrf
<input type="file" name="file">
<button type="submit">Upload</button>
</form>
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.";
}
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');
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:
With this knowledge, you're well-equipped to handle file storage in your Laravel 12 applications. Happy coding!
No comments yet. Be the first to comment!
Please log in to post a comment:
Continue with Google