【问题标题】:Cannot upload image with Intervention image in Laravel 5.2无法在 Laravel 5.2 中上传带有干预图像的图像
【发布时间】:2017-01-06 23:35:26
【问题描述】:

我正在尝试使用教程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()));
    }

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

【问题讨论】:

  • 通过放置 die('QA'); 尝试调试hasFile 中的语句。如果 die() 没有执行你的输入没有文件。如果 die() 被调用,尝试 move_uploaded_file() ---> php.net/manual/en/function.move-uploaded-file.php - 看看你是否可以上传图片
  • 执行成功。用 die 声明
  • OK...还要确保你有使用图像;为此类设置
  • 我一开始就是这么做的。我认为问题可能出在$request-&gt;file('avatar'); 行。
  • 那么 die($avatar->getClientOriginalExtension()) 会出错……是吗?

标签: php laravel file-upload


【解决方案1】:
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()) );

}

【讨论】:

    猜你喜欢
    • 2016-12-19
    • 2017-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多