How to store IMAGES outside public folder

Published 4 months ago by Dev0ps

which is the safest place to store users images and other data.

and how to store and access them in the blade

currently, I am storing and accessing data in public folder

jlrdw
jlrdw
4 months ago (251,320 XP)

With correct folder permissions, you can store images anywhere you want, you are just dealing with a file system, there is no mystery there.

jdc1898

Public is common and appropriate for non-preprocessed resources.

To access those assets from your bade you simply reference it.

Example: {{ asset('images/1.png') }} is the same as public/images/1.png

Dev0ps

consider i want to save images in \resources\uploads\images

then $path = base_path() . 'resources/uploads/images';

and access it via {{ asset('resources/uploads/images/1.png') }}

jdc1898

Someone else may can chime in on whether or not that "will" work. It is my understanding that the resources directory is designed for pre-processed files that will be combined. Like JS or CSS.

As @jlrdw mentioned, it is a filesystem so you can probably do whatever you want.

Thyrosis

I think the real question is: What do you mean by 'safest places'? Do you mean safest for you not to throw them away by accident? Or safest to not include them to your git-repo? Or something else entirely?

Because whether you like it or not, images need to be publically available to be able to be loaded by users in browsers. Sure, don't put them in public but use the storage/assets folder. Happy days. Just don't forget to create a symlink from the public directory to wherever you put them because one way or another, your visitors need to access them. Publically.

Snapey
Snapey
4 months ago (1,036,605 XP)

If you put images in the resources folder (or a sub folder) then they will not be accessible to visitors.

Is that the sort of 'safe' you mean?

If you do this then any images in there will need to be streamed to the client by your code and not directly accessible as assets by the browser

Robstar

@Dev0ps - all is explained in the documentation at https://laravel.com/docs/5.6/filesystem#the-public-disk

If using the public disk files will be physically stored in a none publicly accessible directory. Yiu need to symlink that directory so it's accessible. Laravel has a buiolt in command for that too (again described in the docs):

php artisan storage:link

jasonking92

In Laravel you store your images in a storage folder (by default). If you need to publicly access them, you can set up a alias folder in public via the php artisan storage:link command.

php artisan storage:link

https://laravel.com/docs/5.6/filesystem

You can access them via the following:

{{assets('images/myfile.jpg')}}
Snapey
Snapey
4 months ago (1,036,605 XP)

A few issues here

php artisan storage:link creates a storage folder in public which symlinks to the storage/app/public/ folder

asset() (note it is not assets) is just a url to the content in the public folder (by default)

so, suppose you put landscape.jpg in the storage/app/public folder then it will appear in the public/storage folder and you can generate a URL to it with the asset helper like

<img src="{{ asset('storage/landscape.jpg') }}" /> 

which is actually no different to

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

All this is based on correctly hosting the website with public as the document root

Please sign in or create an account to participate in this conversation.