Skip to content

Instantly share code, notes, and snippets.

@colbyalbo
Created January 1, 2023 22:22
Show Gist options
  • Save colbyalbo/27153a3ce0f8b1582fae8d2ca1811e8a to your computer and use it in GitHub Desktop.
Save colbyalbo/27153a3ce0f8b1582fae8d2ca1811e8a to your computer and use it in GitHub Desktop.
Laravel - Restrict viewing of images from local storage
<?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