Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between storing files in public directory and in storage in Laravel 5.4

Tags:

What's the difference between storing files (images) in public/images folder and storing them in storage/app/public?

like image 546
Камилов Тимур Avatar asked Mar 15 '18 11:03

Камилов Тимур


People also ask

Where should I store files in Laravel?

Laravel's filesystem configuration file is located at config/filesystems.php . Within this file, you may configure all of your filesystem "disks". Each disk represents a particular storage driver and storage location.

What is the purpose of a public folder in Laravel?

The files and folders in laravels public folder are meant to be web accessible. For security, all other files and folders in the laravel framework should not be web accessible.

How do I change storage location in Laravel?

I just got useStoragePath() method inside of Laravel Container. To change storage folder location, just add this code inside bootstrap/app. php and after $app variable initialized. Please sign in or create an account to participate in this conversation.


2 Answers

Public folder means files will be publicly accessible. For example an image stored in public/images/my-image.jpeg can be viewed by anyone by going to

mysite.com/images/my-image.jpeg

However, files stored in storage directory are only available to your app.

Laravel has a php artisan storage:link command that adds a symlink to public from storage/app/public

The reason for this is that your storage may not be your local filesystem, but rather an Amazon S3 bucket or a Rackspace CDN (or anything else)

You will need to setup your filesystem configurations by following the docs https://laravel.com/docs/5.6/filesystem

Once this is done you can get/store files to/from the storage place rather than have everything on your server.

There are 2 helper methods for public and storage to show files:

storage: storage_path('my-file.jpg')

public: asset('my-file.jpg')

like image 141
H H Avatar answered Sep 21 '22 07:09

H H


If you want some control over who can access what files, put the file into storage.

If everyone can access the file (including non logged in users), put into public

like image 28
AddWeb Solution Pvt Ltd Avatar answered Sep 18 '22 07:09

AddWeb Solution Pvt Ltd