Created
January 1, 2023 22:22
-
-
Save colbyalbo/27153a3ce0f8b1582fae8d2ca1811e8a to your computer and use it in GitHub Desktop.
Laravel - Restrict viewing of images from local storage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//confg filesystems | |
'secure_images' => [ | |
'driver' => 'local', | |
'root' => storage_path('app/secure/images'), | |
'url' => env('APP_URL') . '/secure/images', | |
'visibility' => 'private', | |
], | |
//route file | |
Route::get('show-image/{user:username}', [ImageController::class, 'showImage'])->name('show-image'); | |
// blade file | |
<img src="{{route('show-image', $user->username)}}"> | |
//ImageController | |
public function showImage(string $username) | |
{ | |
if (auth()->user()->username !== $username) { | |
abort(404); | |
} | |
$image = Storage::disk('secure_images')->get('pathToImage'); | |
return response($image)->header('Content-Type', 'image'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment