How to access the public Storage directory in Laravel 5.3?

Asked

Viewed 4,352 times

4

I need to access images saved in the directory /storage/app/public in views, but I can’t, I have a 404.

Here’s what I’m doing:

<div class="row">
    <img src="../storage/app/public/3.jpg">
</div>

But he can’t find the image.

Is there any helper of Laravel that accesses this folder?

  • try direct 3.jpg, the public is the root of the project

  • 1

    @Douglascarvalho is actually a briefcase with a public name inside Storage/app

1 answer

2


If storage/public refers to something like /home/user/projeto-em-laravel/public/storage/public I think it would be interesting to change your strategy, the folder storage should not be directly accessed and will not be able to, unless you have done something very wrong, the correct is to use the function asset, thus:

echo asset('storage/file.txt');

According to the documentation https://laravel.com/docs/5.3/filesystem#the-public-disk

The public disk is intended for files that will be publicly accessible. By default, the public disk uses the local disk and stores those files on storage/app/public.

To make them accessible from the web, you can create a symbolic link from public/storage for storage/app/public.

To create the symbiotic link you must execute the command:

php artisan storage:link

Once created, you can create a url and use the "helper" function called asset within a route:

echo asset('storage/file.txt');

You can access it like this (I’m not sure if you have to enter the prefix public/):

<img src="storage/3.jpg">

It is not correct to do this, the public should actually represent the root of an HTTP page, for example in Apache:

DocumentRoot "/home/user/projeto-em-laravel/public"
<Directory "/home/user/projeto-em-laravel/public">
    AllowOverride all
</Directory>

Then the images would be accessible like this:

<img src="3.jpg">

If it’s in the folder /home/user/projeto-em-laravel/public/images/3.jpg, would look like this:

<img src="images/3.jpg">

If you are images/3.jpg background use in a CSS like this /home/user/projeto-em-laravel/public/css/meucss.css then you should use the ../, thus:

seletor {
    background: url(../images/3.jpg);
}

Read more on: /a/91799/3635

  • It’s actually a folder with a public name inside Storage/app

  • Storage is a folder just to store data, you should redirect it as route preferably, I will try to do something like Windows or directly in . htaccess.

  • 1

    Directory is ./app/storage/app/public/3.jpg, the problem is I can’t access it from the view

  • @Felipepaetzold edited response

Browser other questions tagged

You are not signed in. Login or sign up in order to post.