Laravel Storage::disk()
– File Storage Made Easy
One of Laravel's most powerful features is its flexible storage system. The the Storage::disk()
method allows you to work with different file systems—whether it’s local storage, public folders, or cloud services like Amazon S3.
What is Storage::disk()
?
The the disk()
method retrieves a file system instance for a given disk configuration. A disk refers to a specific storage driver and location, such as:
-
Local filesystem
-
Public directory
-
Amazon S3 (cloud)
You can then use this $disk
instance to perform operations like creating, reading, updating, and deleting files.
Configuration: config/filesystems.php
-
local is the default disk.
-
public is for publicly accessible files.
-
s3 integrates with Amazon S3 cloud storage.
⚠️ Use the following command to create a symbolic link to access public files:
Example 1: Upload File to Local Disk
Step 1: Laravel Setup
Step 2: Database Setup
Create a database named laravel_demo_blog
:
Step 3: Migration Schema
Edit the migration file in database/migrations/*_create_blogs_table.php
:
Run the migration:
Step 4: Blade View (resources/views/create.blade.php
)
Step 5: Routes (routes/web.php
)
Step 6: Controller Logic
Example 2: Upload File to Amazon S3
Laravel Storage Cheat Sheet
Command | Description |
---|---|
Storage::disk('local')->delete('example.txt') | Delete file |
Storage::disk('local')->deleteDirectory('folder') | Delete directory |
$files = Storage::disk('local')->files('folder') | List files |
Storage::disk('local')->exists('file') | File existence |
Storage::disk('s3')->put('file', 'content') | Upload to S3 |
Storage::disk('s3')->url('file') | Get S3 file URL |
Storage::disk('local')->get('file') | Read file |
Storage::disk('local')->size('file') | Get file size |
Storage::disk('local')->lastModified('file') | Last modified time |
Storage::disk('local')->append('file', 'text') | Append text |
Storage::disk('local')->makeDirectory('folder') | Create directory |
Conclusion
Laravel's Storage::disk()
method unifies file management across various storage options like local, public, and cloud services (Amazon S3, FTP, etc.). Whether you're building a blog, uploading documents, or handling media, Laravel provides a consistent and easy-to-use API for managing your file storage.