无法在Laravel 5.2中使用干预图像上传图像

时间:2016-08-30 15:30:57

标签: php laravel file-upload

我正在尝试使用教程here上传图片。我完全按照视频中的说明操作,但是一旦我点击提交按钮,它就会将我带回到个人资料页面,图像没有变化,数据库中没有任何变化或上传到指定的文件夹。

这是我的routes.php

Route::get('profile', 'UserController@showProfile');
Route::post('profile', 'UserController@updateAvatar');

这是刀片文件:

<div class="row">
   <div class="col-sm-4">
       <div class="text-align-center">
           <img class="img-circle" src="/uploads/avatars/{{ Auth::user()->avatar }}" alt="64x64" style="height: 112px; border-radius:50%;">
       </div>
       <br>
       <br>
       <div class="text-align-center">
          <form enctype="multipart/form-data" action="{{ url('/profile') }}" method="POST">
          <h5>Update Profile Image</h5>
          <input type="file" name="avatar">
          <input type="hidden" name="_token" value="{{ csrf_token() }}"> <br>          
          <input type="submit" class="pull-left btn btn-sm btn-primary">
          </form>
       </div>
</div>

这是控制器:

 public function showProfile(){

        return view('profile', array('user' => Auth::user()));
    }

    public function updateAvatar(Request $request){
        //Handle the user upload of avatar
        if($request->hasFile('avatar')){
            $avatar = $request->file('avatar');
            $filename = time() . '.' . $avatar->getClientOriginalExtension();
            Image::make($avatar)->resize(200, 200)->save( public_path('/uploads/avatars/' . $filename ) );
            $user = Auth::user();
            $user->avatar = $filename;
            File::
            $user->save();
        }

        return view('profile', array('user' => Auth::user()));
    }

我在avatar表中添加了一个字段users

1 个答案:

答案 0 :(得分:0)

public function update_avatar(Request $request){

    // Handle the user upload of avatar
    if($request->hasFile('avatar')){
        $avatar = $request->file('avatar');
        $filename = time() . '.' . $avatar->getClientOriginalExtension();
        $avatar->move('/uploads/avatars', $filename);       
        $user = Auth::user();
        $user->avatar = $filename;
        $user->save();
    }

    return view('profile', array('user' => Auth::user()) );

}