我使用的是laravel-5.4make:auth
。在register.blade.php
中,为用户添加了一个额外的字段配置文件图片。
<form class="form-horizontal" role="form" method="POST" action="{{ route('register') }}" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('image') ? ' has-error' : '' }}">
<label for="image" class="col-md-4 control-label"> Profile picture</label>
<div class="col-md-6">
<input id="image" type="file" class="form-control" name="image">
@if ($errors->has('image'))
<span class="help-block">
<strong>{{ $errors->first('image') }}</strong>
</span>
@endif
</div>
</div>
我想将图像路径存储在数据库中。我还执行了:
php artisan storage:link
和[public/storage]
目录已链接。app\http\controllers\auth\registercontroller.php:
public function store(Request $request)
{
if($request->hasFile('image')) {
$image_name = $request->file('image')->getClientOriginalName();
$image_path = $request->file('image')->store('public');
$image = Image::make(Storage::get($image_path))->resize(320,240)->encode();
Storage::put($image_path,$image);
$image_path = explode('/',$image_path);
$user->image = $image_path;
$user->save();
} else{
return "No file selected";
}
}
web.php网站
Route::post('/store', 'RegisterController@store');
Route::get('/show', 'RegisterController@show');
在数据库中,映像下的in user表存储为临时路径:
C:\xampp\tmp\phpc762.tmp。
如何存储
storage\app\public
的图像路径。 最佳答案
在控制器中更改此代码
$user->image = $image_path;
到
$user->image = Storage::url($image_name);
文件:https://laravel.com/docs/5.4/filesystem#file-urls
关于database - 如何在Laravel-5.4中存储storage\app\public的镜像路径,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43439759/