如何以及在哪里可以使用 Laravel 存储图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38256539/
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
how and where can store images with laravel?
提问by Reco Jhonatan
how and where I can store images using laravel?
我如何以及在哪里可以使用 Laravel 存储图像?
context: I want display some images in my main view (these are predefined by me). I also want to store images uploaded by users, however, do not know where to store these images.
上下文:我想在我的主视图中显示一些图像(这些是我预定义的)。我也想存放用户上传的图片,但是不知道这些图片存放在哪里。
I'd like to call these from the view...
我想从视图中调用这些...
Thank you very much for the help. :)
非常感谢你的帮助。:)
回答by Carter
Basically you can save your files wherever you want within your Laravel application provided that you have permissions to create directory and file.
基本上,只要您有创建目录和文件的权限,您就可以在 Laravel 应用程序中的任何位置保存文件。
But I prefer saving files in the storage/app
folder. Laravel provides a simple API to manipulate files on disk. See the docs.
但我更喜欢将文件保存在storage/app
文件夹中。Laravel 提供了一个简单的 API 来操作磁盘上的文件。请参阅文档。
Update:
更新:
This answer was posted when Laravel was in version 5.2.
From version 5.3 on, you can easily create symbolic links by running the artisan command php artisan storage:link
. See the docs.
这个答案是在 Laravel 5.2 版本时发布的。从 5.3 版开始,您可以通过运行 artisan 命令轻松创建符号链接php artisan storage:link
。请参阅文档。
回答by Reco Jhonatan
If you want to display them on your site store them in your public directory. Since they are uploaded by users you will need to pass them through a form. Here is an example of a controller.
如果您想在您的站点上显示它们,请将它们存储在您的公共目录中。由于它们是由用户上传的,因此您需要通过表单传递它们。这是控制器的示例。
$file = Input::file('picture');
$file->move(public_path().'/images/',$user->id.'.jpg');
The user will submit a form with a picture field. The two lines above will store it in the public directory in an images folder, with the relevant user's id as its name. Your probably best off making a model in your database for images and their paths. If you do, add these lines after the two above.
用户将提交一个带有图片字段的表单。上面两行将它存储在图像文件夹中的公共目录中,并以相关用户的 id 作为其名称。您最好在数据库中为图像及其路径制作模型。如果这样做,请在上述两行之后添加这些行。
$image = new Image;
$image->path='/images/'.$user->id.'.jpg';
$image->user_id = $user->id;
$image->save();
To display it in the view simply set an $image variable to the correct image model in your controller and pass it to the view. Then pop its path in the src of the image.
要在视图中显示它,只需将 $image 变量设置为控制器中正确的图像模型并将其传递给视图。然后在图像的 src 中弹出它的路径。
<img src={{$image->path}} alt={{$image->path}}>
回答by Manjitha teshara
Make directory for images in myapp/public/images and keep images on there.
在 myapp/public/images 中创建图像目录并将图像保存在那里。
<img src="{{URL('/images/logo.jpg')}}">