如何使用 storage_path() 在 Laravel 4 中查看图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17029121/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:56:11  来源:igfitidea点击:

How to use storage_path() to view an image in laravel 4

laravellaravel-4

提问by Bryan P

I'm using storage_path()for storing uploaded images, but when I use it is pointing wrong on my page.

storage_path()用于存储上传的图像,但是当我使用它时,我的页面指向错误。

I use it like this {{ $data->thumbnail }}where $datacame from the database and thumbnail comes as the string which used storage_path

我使用这样的{{ $data->thumbnail }}地方$data从数据库来和缩略图来作为使用的字符串storage_path

回答by Marco Rivadeneyra

The storage_path function returns the path to the storage folder, which is inside the app folder --and outside the public folder-- so it's not directly accessible from the client, that's why your images are not being displayed. You can move them to the public folder path, or you could use a custom controller to handle the image requests, read the image from the storage folder and return the value.

storage_path 函数返回存储文件夹的路径,该文件夹位于 app 文件夹内 - 位于公共文件夹之外 - 因此无法从客户端直接访问,这就是您的图像未显示的原因。您可以将它们移动到公共文件夹路径,或者您可以使用自定义控制器来处理图像请求,从存储文件夹中读取图像并返回值。

回答by Remluben

Let us take a look at the default L4 application structure:

让我们来看看默认的 L4 应用程序结构:

app // contains restricted server-side application data

app // 包含受限的服务器端应用程序数据

app/storage // a writeable directory used by L4 and custom functions to store data ( i.e. log files, ... )

app/storage // L4 和自定义函数使用的可写目录来存储数据(即日志文件,...)

public // this directory is accessible for clients

public // 客户端可以访问这个目录

If I were you, I would upload the file to the public directory directly:

如果我是你,我会直接将文件上传到公共目录:

  1. Store image here: public_path() . 'img/filename.jpg'
  2. Save the 'img/filename.jpg' in database
  3. Generate the image URL with url('img/filename.jpg') => http://www.your-domain.com/img/filename.jpg
  1. 在此处存储图像: public_path() 。'img/文件名.jpg'
  2. 将 'img/filename.jpg' 保存在数据库中
  3. 使用 url('img/filename.jpg') => http://www.your-domain.com/img/filename.jpg生成图像 URL

Hope this helps.

希望这可以帮助。