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

大憨熊 提交于 2019-12-05 02:07:12

问题


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

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


回答1:


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

app // contains restricted server-side application data

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

public // this directory is accessible for clients

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

Hope this helps.




回答2:


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.



来源:https://stackoverflow.com/questions/17029121/how-to-use-storage-path-to-view-an-image-in-laravel-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!