BrianA's avatar

Loading An Image From Storage In Blade View (Protected)

Hi,

I need to show an image in a Blade view , however, I do not want the images to be public. Only the users who need to see a particular image can have access to it.

This is what I was doing before realizing that the images must not be accessible by anyone if the path is known/guessed:

  • I was storing images (which are uploaded by an admin) in storage/app/public/uploads/iotpark/warranty/

  • The same images were available in public/storage/... (I ran php artisan storage:link)

  • In my Bade view, I was getting and displaying the image using <img src="<?php echo asset("storage/uploads/iotpark/warranty/$currentFile")?>"></img>

I would now like to store my images in storage/uploads/iotpark/warranty/ instead.

  • What is the best way of displaying an image (which is not public) in a Blade view please? I think the ideal would be to pass the specific file from the controller to the blade view if possible.

Note: I have the Image Intervention package installed, so maybe I could use this package for this application/case?

Thanks in advance, Brian

0 likes
2 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

The answer is exactly what you suggested yourself. Use a controller with a guard/policy

This is just dummy code. Redo with yourself with proper handling for getting the filepath

public function image($filename)
{
$this->authorize('view-image');

$filePath = Storage::path('basepath' . $filename);
return response()->file($filePath);
}
1 like
BrianA's avatar

Hi @sinnbeck

That's what I did and it is working well, as I wanted now. I'm using a controller (the route is protected by middleware) to 'return' the image to the view.

Thanks! :)

Please or to participate in this conversation.