I'm working with laravel 5.1
, I'm trying to upload a file by my form and move to folder images/user/id user, I'm trying like this:
VIEW
<form method="post" action="{{url('account/update/avatar')}}" enctype="multipart/form-data" class="form-horizontal" role="form">
{csrf_field()}}
<input id="input-upload-img1" type="file" class="file" data-preview-file-type="text">
<button type="submit" class="btn btn-success">Cambia</button>
</form>
MY CONTROLLER
public function updateAvatar(Request $request){
if ($request->hasFile('image')){
$username = Auth::user()->id . '.' . $request->file('image')->getClientOriginalExtension();
$request->file('image')->move('images/users/', $username);
$user = new User;
$user->where('email', '=', Auth::user()->email)
->update(['image' => 'images/users/'.$username]);
return redirect('account')->with('message-success', 'success!');
}else{
return redirect('account')->with('message-error', 'File error');
}
}
ROUTES
Route::post('account/update/avatar', 'UserController@updateAvatar');
Actually it return -->
return redirect('account')->with('message-error', 'File error');
尝试在输入中添加名称
<input id="input-upload-img1" type="file" class="file" data-preview-file-type="text" name="image">
<input id="input-upload-img1" type="file" name="image" class="file" data-preview-file-type="text">
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.