【发布时间】:2017-07-18 14:27:36
【问题描述】:
我需要为头像保存图片。
谁能给我一个简单的代码来保存和检索图像?
我需要:
- 将图像保存在文件夹中
- 在数据库中保存图像名称
- 终于在图片标签上检索;我必须通过查询生成器来完成
表格:
<form action="" method="post" role="form" multiple>
{{csrf_field()}}
<legend>Form Title</legend>
<div class="form-group">
<label for="">Your Image</label>
<input type="file" name="avatar">
</div>
<button type="submit" class="btn btn-primary">save</button>
<a href="{{'/'}}" class="btn btn-primary">back</a>
</form>
<img name="youravatar" src="">
</div>
路线:
Route::get('pic','avatarController@picshow');
Route::post('pic','avatarController@pic');
控制器:
我有avatarController,但它是空的,因为我不知道该怎么做。
数据库:
表格名称:头像
字段:名称 id、imgsrc、created_at、Updated_at
其他:
我找到了这段代码,但我什么也找不到:
if ($request->hasFile('avatar')) {
$file = array('avatar' => Input::file('avatar'));
$destinationPath = '/'; // upload path
$extension = Input::file('avatar')->getClientOriginalExtension();
$fileName = rand(11111,99999).'.'.$extension; // renaming image
Input::file('avatar')->move($destinationPath, $fileName);
}
【问题讨论】:
-
到目前为止你有什么(控制器、表单、数据库结构)
-
帖子已编辑。 tnx老兄
-
尝试在
-
是的,我看到了,但我几乎什么都做不了。
标签: mysql database image laravel file-upload