简体   繁体   English

在Laravel 5.4中在公共目录和存储中存储文件之间的区别

[英]Difference between storing files in public directory and in storage in Laravel 5.4

将文件(图像)存储在public/images文件夹中并将它们storage/app/publicstorage/app/public什么区别?

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 例如,任何人都可以通过访问来查看存储在public/images/my-image.jpeg

mysite.com/images/my-image.jpeg

However, files stored in storage directory are only available to your app. 但是,存储在storage目录中的文件仅适用于您的应用程序。

Laravel has a php artisan storage:link command that adds a symlink to public from storage/app/public Laravel有一个php artisan storage:link命令,可以从storage/app/publicpublic添加符号链接

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) 原因是您的storage可能不是您的本地文件系统,而是Amazon S3存储桶或Rackspace CDN(或其他任何东西)

You will need to setup your filesystem configurations by following the docs https://laravel.com/docs/5.6/filesystem 您需要按照文档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: publicstorage有两种帮助方法来显示文件:

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

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

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 如果每个人都可以访问该文件(包括未登录的用户),请公开

The public/images is a webroot directory. public/images是一个webroot目录。 This means that it can be accessed via a web browser mozilla, chrome, etc... 这意味着它可以通过网络浏览器mozilla, chrome, etc...

The storage/app/public is a folder for cache , logs . storage/app/publiccache logs的文件夹。

Where to place my files? 在哪里放置我的文件?

Rule of thumb: If you need to control who can view those files put them in storage/app/public otherwise put them in public/images 经验法则:如果你需要控制谁可以查看这些文件,将它们放在storage/app/public否则将它们放在public/images

EDIT 编辑

As other answers pointed out the public webroot directory any user can see it. 正如其他答案指出public webroot目录任何用户都可以看到它。 Even non logged users 即使是非登录用户

A similar question was asked here and was answered stating: 在这里提出一个类似的问题,并回答说:

public is a "WEBROOT" directory. public是一个“WEBR​​OOT”目录。 it consists of files which can be accessed from a browser. 它由可以从浏览器访问的文件组成。 There is your index.php file, which take a role of your enter point. 有你的index.php文件,它起到你的输入点的作用。 Also your css , javascript files there. 还有你的cssjavascript文件。

storage is a folder for cache, logs etc. storage是缓存,日志等的文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM