我正在为我的项目创建一个头像上传系统。我似乎无法解决这个错误......
这是代码
public function update_avatar(Request $request){
// Handle the user upload of avatar
if($request->hasFile('avatar')) {
$avatar = $request->file('avatar');
$filename = time() . '.' . $avatar->getClientOriginalExtension();
$filelocation = public_path('/uploads/avatars/' . $filename );
Image::make($avatar->getRealPath())->resize(300, 300)->save($filelocation);
$user = Auth::user();
$user->avatar = $filename;
$user->save();
}
return view('user.profile', array('user' => Auth::user()) );
}
这是错误
in AbstractDecoder.php line 339
at AbstractDecoder->init(false) in AbstractDriver.php line 64
at AbstractDriver->init(false) in ImageManager.php line 50
at ImageManager->make(false) in Facade.php line 221
at Facade::__callStatic('make', array(false)) in ProfileController.php line 26
at Image::make(false) in ProfileController.php line 26
at ProfileController->update_avatar(object(Request))
at call_user_func_array(array(object(ProfileController), 'update_avatar'), array(object(Request))) in Controller.php line 55
at Controller->callAction('update_avatar', array(object(Request))) in ControllerDispatcher.php line 44
at ControllerDispatcher->dispatch(object(Route), object(ProfileController), 'update_avatar') in Route.php line 204
at Route->runController() in Route.php line 160
at Route->run() in Router.php line 559
at Router->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 30
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in SubstituteBindings.php line 41
at SubstituteBindings->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in VerifyCsrfToken.php line 65
at VerifyCsrfToken->handle(object(Request), object(Closure)) in Pipeline.php line 148
无论如何都是错误......
谢谢你们
答案 0 :(得分:0)
根据错误日志,方法getRealPath()
正在返回false
。在执行任何操作之前,您可能想要检查文件是否有效。您可以通过isValid()
方法完成此操作。
此外,您的代码似乎过于复杂,无法存储上传的照片。存在这样做的方法,您可能想要使用此方法:storeAs('images', 'filename.jpg'