我一直在做我的项目,但无法了解Laravel Storage,特别是visibility : public
存储状态及其URL。
我通过$filename = $file->store('mpmlimages', 'public');
保存上传的文件
据我所知,当我把这个$filename
放在asset()
助手函数中时,我就会得到我的文件URL。
在当前的目录结构中,
projectroot_folder
|-- storage <-- this is actual storage, right?
| |--app
| |--public
| |--mpmlimages <-- this is file location
|--public
|--storage (symlink to storageapppublic) <-- it has shortcut icon.. I believe it's what laravel said simbolic link
如果
我使用asset($filename)
,我得到了http://localhost:8000/mpmlimages/{GENERATED_FILENAME}.png
,则无法工作
如果
手动将上述URL更改为http://localhost:8000/storage/mpmlimages/{GENERATED_FILENAME}.png
则有效
为什么?!?!?!?!?!?我不明白我错过了什么。
最简单的解决方案是为资产调用准备存储:
asset("storage/{$filename}")
这样你就不必创建任何符号链接(除了你已经拥有的链接(。为了回答最初的问题:调用$file->store
将使用公共磁盘。如果您检查配置文件,您会注意到公共磁盘的根目录是'root' => storage_path('app/public')
,所以这就是为什么您的文件位于storage/app/public
文件夹中的原因。
此外,作为客户端,您应该只能访问公用文件夹中的文件,因此有了指向storage
的符号链接。这就是不使用storage_path()
的原因,因为它将公开Web服务器结构,而不是创建客户端可以使用的url。示例:
asset('storage/{$filename}')
导致https:/server.com/storage/file.png
storage_path($filename)
导致/var/www/server/storage/app/public/file.png
这是因为asset()
将指向public
文件夹
通过创建从storage
到public
的symbolic link
并使用asset()
,您仍然需要告诉laravel您希望访问公共文件夹/文件中的哪个。
在您的情况下,是您的symbolic
链接which will restructure the path to
存储\app\public`;
如果你从storageapppublicmpmlimages
到publicmpmlimages
做一个symbolic link
,你可以保留为asset('mpmlimages/<your img>')
我认为您的符号链接结构是
root
|--public
| |--**S**storage (storageapppublic)
|
|--storage
|--app
|--public
|--mpmlimages
|--img1.png
|--img2.jpeg
|-- etc
您的符号链接应该是什么才能丢弃存储
root
|--public
| |--**S**mpmlimages(storageapppublicmpmlimages)
|
|--storage
|--app
|--public
|--mpmlimages
|--img1.png
|--img2.jpeg
|-- etc
图例:
S->作为Symlink指标
要实现这种符号链接关系,请运行:
ln -s storage/app/public/mpmlimages public/mpmlimages
如果你的$filename
包含mpmlimages/<img>
,你只需要:
asset($filename);