php Laravel 5.4 存储链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50185273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.4 Storage link
提问by mouhamed slaimi
i would like to show a picture in html file i go to the link folder public/storage/image is empty but in the storage/image i find the file and its also saved in the data base.*
我想在 html 文件中显示一张图片,我转到链接文件夹 public/storage/image 是空的,但在 storage/image 中我找到了该文件,并且它也保存在数据库中。*
@foreach($cvs as $cv)
<div class="col-sm-6 col-md-4">
<div class="thumbnail">
<img src="{{ asset('storage/'.$cv->photo) }}" alt="...">
<div class="caption">
<h3>{{ $cv->titre }}</h3>
<p>{{$cv->presentation}}</p>
<p>
<a href="#" class="btn btn-primary" role="button">Afficher</a>
<a href="#" class="btn btn-success" role="button">Modifier</a>
<a href="#" class="btn btn-danger" role="button">Supprimer</a>
</p>
</div>
</div>
</div>
@endforeach
</div>
</div>
回答by Mayank Majithya
The public
disk is intended for files that are going to be publicly accessible. By default, the public
disk uses the local driver and stores these files in storage/app/public
. To make them accessible from the web, you should create a symbolic link from public/storage
to storage/app/public
. Laravel doc
该public
磁盘用于将要公开访问的文件。默认情况下, public
磁盘使用本地驱动程序并将这些文件存储在storage/app/public
. 要使它们可从 Web 访问,您应该创建一个从public/storage
到 的符号链接storage/app/public
。Laravel 文档
Create Symbolic link in Linux
在Linux 中创建符号链接
ln -s source_file myfile
回答by Aamir Kalimi
First of all in the .env filecreate a variable named FILESYSTEM_DRIVERand set it to public
like this
首先在.env 文件中创建一个名为FILESYSTEM_DRIVER的变量并将其设置为public
这样
FILESYSTEM_DRIVER=public
and then create symbolic link by run
然后通过运行创建符号链接
php artisan storage:link
after that just use asset()
之后只需使用资产()
<img src="{{ asset('storage/'.$cv->photo) }}" alt="...">
It'll work fine
它会工作得很好
回答by mouhamed slaimi
solved it with just a simple modification on the filesystems config/file systems from :
只需对以下文件系统配置/文件系统进行简单修改即可解决:
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
]
to :
到 :
'local' => [
'driver' => 'local',
'root' => storage_path('app/public'),
]
回答by Jacob Runge
As Mayank Majithya notes, a symbolic link is one common way to access storage/app/public/
files from the public/
directory, and in fact the method the Laravel docssay you should use.
正如 Mayank Majithya 指出的那样,符号链接是storage/app/public/
从public/
目录访问文件的一种常用方法,实际上Laravel 文档中说你应该使用这种方法。
If you are on shared hosting that disallows this, you could also try to use Laravel's storage_path()
helper function, like so: <img src="{{ storage_path('subpaths/under/storage/filename.jpg') }}">
(reference).
如果你是在共享主机是不允许这个,你也可以尝试使用Laravel的storage_path()
辅助函数,像这样:<img src="{{ storage_path('subpaths/under/storage/filename.jpg') }}">
(参考)。
I also notice you mention the images are stored in the database. If you mean the whole file is stored, it may be more efficient (once you have the file path figured out) to store only the file path in your database, rather than the whole file (since it is already stored on disk). You can then use your model to get the path (assuming your table has columns for, say, 'src' and 'alt'):
我还注意到您提到图像存储在数据库中。如果您的意思是存储整个文件,那么仅在数据库中存储文件路径而不是整个文件(因为它已经存储在磁盘上)可能更有效(一旦您确定了文件路径)。然后你可以使用你的模型来获取路径(假设你的表有列,比如“src”和“alt”):
<img src="{{ $mymodel->src }}" alt="{{ $mymodel->alt }}">
回答by Carlos R
This worked for me.I added the command "Artisan::call('storage:link', [] );" to the beginning of the route file and deleted the storage folder from public.
这对我有用。我添加了命令“Artisan::call('storage:link', [] );” 到路由文件的开头,并从 public 中删除了存储文件夹。
It is important to delete the folder first, otherwise it will not work.
首先删除文件夹很重要,否则将无法正常工作。
Removed the code from the route, if it is working, since it should only run once on the server.
从路由中删除代码(如果它有效),因为它应该只在服务器上运行一次。