Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vincent15000's avatar

Access images only if authenticated

Hello,

I need to let users access images only if they are authenticated.

So I don't create any public symlink.

I think that I have to create a controller and display the image from a route.

<img src="{{ route('media', ['path' => $user->avatar_thumb_url]) }}">
...
$path = base_path().$path;

return response()->file(storage_path($path));

But it doesn't work.

What's wrong with my code ?

Is there another way, perhaps simpler, to do the same ?

And something disturbs me : the full path to the image (/home/vincent/...) is visible in the source code, and I don't want the path to be visible. So I'd like to create to create either a sort of temporary link or a slug to access each image.

What do you suggest me ?

Thanks a lot ;).

Vincent

0 likes
3 replies
vincent15000's avatar
vincent15000
OP
Best Answer
Level 63

I just found the solution.

// Route
Route::get('media/{media}', MediaController::class)->name('media');
...
// MediaController
return response()->file($media->getPath('thumb'));
...
// Blade view
<img src="{{ route('media', ['media' => $user->avatar]) }}">
jlrdw's avatar

Do you have authorization also, meaning can only the user who created the image view the image and download the image.

1 like

Please or to participate in this conversation.