Laravel storage::disk [In-Depth Tutorial]

Laravel storage::disk [In-Depth Tutorial]

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)

$disk = Storage::disk('local');

You can then use this $disk instance to perform operations like creating, reading, updating, and deleting files.

Configuration: config/filesystems.php

return [ 'default' => env('FILESYSTEM_DISK', 'local'), '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'), 'endpoint' => env('AWS_ENDPOINT'), 'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false), ], ], 'links' => [ public_path('storage') => storage_path('app/public'), ], ];
  • 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:

php artisan storage:link

Example 1: Upload File to Local Disk

Step 1: Laravel Setup

laravel new laravel_demo_blog cd laravel_demo_blog php artisan make:model Blog -mcr code .

Step 2: Database Setup

Create a database named laravel_demo_blog:

mysql -u root -p create database laravel_demo_blog; exit;

Step 3: Migration Schema

Edit the migration file in database/migrations/*_create_blogs_table.php:

Schema::create('blogs', function (Blueprint $table) { $table->id(); $table->string('title'); $table->string('image'); $table->text('body'); $table->timestamps(); });

Run the migration:

php artisan migrate

Step 4: Blade View (resources/views/create.blade.php)

<x-layout> <form action="{{ url('store') }}" method="POST" enctype="multipart/form-data" class="w-2/4 mx-auto"> @csrf <div class="mb-4"> <label for="title" class="block mb-2">Title:</label> <input type="text" name="title" class="w-full bg-gray-100 text-gray-700 p-2 rounded focus:outline-none sm:w-4/5"> </div> <div class="mb-4"> <label for="body" class="block mb-2">Body:</label> <textarea name="body" rows="7" class="w-full bg-gray-100 text-gray-700 p-2 rounded focus:outline-none sm:w-4/5"></textarea> </div> <div class="mb-4"> <input type="file" name="file"> </div> <div class="mb-4"> <button class="bg-cyan-800 hover:bg-cyan-900 rounded px-8 py-2">Store</button> </div> </form> </x-layout>

Step 5: Routes (routes/web.php)

use App\Http\Controllers\BlogController; Route::get('/', [BlogController::class, 'index']); Route::get('create', [BlogController::class, 'create']); Route::post('store', [BlogController::class, 'store']);

Step 6: Controller Logic

use Illuminate\Support\Facades\Storage; public function store(Request $request) { $img = $request->file('file'); $img_name = $img->getClientOriginalName(); $blog = new Blog(); $blog->title = $request->title; $blog->body = $request->body; $blog->image = $img_name; Storage::disk('public')->put('images/' . $img_name, file_get_contents($img)); $blog->save(); return redirect('/'); }

Example 2: Upload File to Amazon S3

public function uploadToS3(Request $request) { $file = $request->file('file'); $s3 = Storage::disk('s3'); $filename = uniqid() . '.' . $file->getClientOriginalExtension(); $s3->putFileAs('uploads', $file, $filename); $url = $s3->url('uploads/' . $filename); return response()->json(['url' => $url]); }

Laravel Storage Cheat Sheet

CommandDescription
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.

Souy Soeng

Souy Soeng

Hi there 👋, I’m Soeng Souy (StarCode Kh)
-------------------------------------------
🌱 I’m currently creating a sample Laravel and React Vue Livewire
👯 I’m looking to collaborate on open-source PHP & JavaScript projects
💬 Ask me about Laravel, MySQL, or Flutter
⚡ Fun fact: I love turning ☕️ into code!

Post a Comment

CAN FEEDBACK
close