上传图片并访问 laravel 5
upload image and access laravel 5
我在laravel 5工作,我想上传用户的头像。
我的上传工作正常,但我不知道如何将图像放到文件夹中。
有人可以帮助我吗?
控制器
public function updateProfile() {
$profileData = Input::except('_token');
$validation = Validator::make($profileData, User::$profileData);
if ($validation->passes()) {
$file = array_get($profileData,'imagem');
$destinationPath = 'imagens/perfil';
$extension = $file->getClientOriginalExtension();
$fileName = rand(11111, 99999) . '.' . $extension;
$upload_success = $file->move($destinationPath, $fileName);
User::where('id', Input::get('id'))->update($profileData);
return view('backend/perfil.index')->with('message', 'Updated Succesfully');
} else {
return view('backend/perfil.index')->with('message', $validation->messages());
}
}
路线
Route::post('backend/perfil', 'BackendControlador@updateProfile');
索引
<div class="col-md-3 col-lg-3 " align="center">
<img alt="User Pic" src="{{ Auth::user()->imagem}}" class="img-circle img-responsive">
</div>
{!! Form::open(array('class' => 'form-horizontal', 'url' => 'backend/perfil', 'name' => 'updateProfile', 'role' => 'form', 'files'=> true))!!}
<input type="hidden" name="id" value="{{Auth::user()->id}}">
<div class="row" style="margin-bottom: 20px;">
<div class="col-md-3 col-lg-3"></div>
<div class="col-md-2 col-lg-2">
{!! Form::label('imagem', 'Imagem', ['class' => 'label_perfil']) !!}
</div>
<div class="col-md-5 col-lg-5">
{!! Form::file('imagem', ['class' => 'input-file']) !!}
</div>
</div>
<div class="row" style="margin-bottom: 20px; margin-top: 30px;">
<div class="col-md-3 col-lg-3"></div>
<div class="col-md-9 col-lg-9">
{!! Form::submit('Alterar perfil', ['class' => 'btn btn-primary']) !!}
</div>
</div>
{!! Form::close() !!}
根据操作员的评论,我猜操作员没有在数据库中保存图像的路径。
你怎么做到的
第 1 步:
每次保存图片时,将图片的路径以某种身份保存在数据库中。
第 2 步:
从数据库中检索图像的路径并将其放在 img src
标签中
即
$yourImagePath = 'xyz.jpg'; # Retrieved from DB
然后
<img src='<?php echo $yourImagePath?>'>
代替这一行
$destinationPath = 'imagens/perfil';
试试下面的代码
$destinationPath = publicpath().'imagens/perfil';
我在laravel 5工作,我想上传用户的头像。 我的上传工作正常,但我不知道如何将图像放到文件夹中。 有人可以帮助我吗?
控制器
public function updateProfile() {
$profileData = Input::except('_token');
$validation = Validator::make($profileData, User::$profileData);
if ($validation->passes()) {
$file = array_get($profileData,'imagem');
$destinationPath = 'imagens/perfil';
$extension = $file->getClientOriginalExtension();
$fileName = rand(11111, 99999) . '.' . $extension;
$upload_success = $file->move($destinationPath, $fileName);
User::where('id', Input::get('id'))->update($profileData);
return view('backend/perfil.index')->with('message', 'Updated Succesfully');
} else {
return view('backend/perfil.index')->with('message', $validation->messages());
}
}
路线
Route::post('backend/perfil', 'BackendControlador@updateProfile');
索引
<div class="col-md-3 col-lg-3 " align="center">
<img alt="User Pic" src="{{ Auth::user()->imagem}}" class="img-circle img-responsive">
</div>
{!! Form::open(array('class' => 'form-horizontal', 'url' => 'backend/perfil', 'name' => 'updateProfile', 'role' => 'form', 'files'=> true))!!}
<input type="hidden" name="id" value="{{Auth::user()->id}}">
<div class="row" style="margin-bottom: 20px;">
<div class="col-md-3 col-lg-3"></div>
<div class="col-md-2 col-lg-2">
{!! Form::label('imagem', 'Imagem', ['class' => 'label_perfil']) !!}
</div>
<div class="col-md-5 col-lg-5">
{!! Form::file('imagem', ['class' => 'input-file']) !!}
</div>
</div>
<div class="row" style="margin-bottom: 20px; margin-top: 30px;">
<div class="col-md-3 col-lg-3"></div>
<div class="col-md-9 col-lg-9">
{!! Form::submit('Alterar perfil', ['class' => 'btn btn-primary']) !!}
</div>
</div>
{!! Form::close() !!}
根据操作员的评论,我猜操作员没有在数据库中保存图像的路径。
你怎么做到的
第 1 步:
每次保存图片时,将图片的路径以某种身份保存在数据库中。
第 2 步:
从数据库中检索图像的路径并将其放在 img src
标签中
即
$yourImagePath = 'xyz.jpg'; # Retrieved from DB
然后
<img src='<?php echo $yourImagePath?>'>
代替这一行
$destinationPath = 'imagens/perfil';
试试下面的代码
$destinationPath = publicpath().'imagens/perfil';